多循环页面上的WP_QUERY分页中断WP或未显示

时间:2013-08-01 作者:canacast

下面的循环是自定义页面模板上的几个循环之一,但它是我尝试实现分页的唯一循环。

一旦我为这一个循环进行了分页,我将实现InfiniteScroll,我相信这比首先安装它要容易得多。。。

我已经读了好几英里的书,但我的大脑还没有找到正确的信息来让它发挥作用。

设计主题是一回事,但工程循环仍然是我难以解决的问题。

    <div class="thirdLeft">
            <?php
            global $paged;
            $curpage = $paged ? $paged : 1;
            $args = array(
                \'post_type\' => \'location\',
                \'orderby\' => \'post_date\',
                \'posts_per_page\' => 5,
                \'paged\' => $paged
            );
            $first_query = new WP_Query(\'cat=2,-20&showposts=18&offset=2\');
            while($first_query->have_posts()) : $first_query->the_post();
            ?>

                <?php get_template_part( \'content\', \'single\' ); ?>  
            <?php endwhile; ?>
    </div>

UPDATE

在浏览完网页后,我发现了一个很棒的图坦卡门:http://weblogtoolscollection.com/archives/2008/04/19/paging-and-custom-wordpress-loops/

设置上面的循环格式以工作并获得其应有的分页,更新版本如下所示:

(除了自定义类别外,与tut相比变化不大)

    <?php add_filter(\'post_limits\', \'my_post_limit\'); ?>
    <div class="thirdLeft">
            <?php
                global $myOffset;
                $myOffset = 2;
                $temp = $wp_query;
                $wp_query= null;
                $wp_query = new WP_Query();
                $wp_query->query(\'offset=\'.$myOffset.\'&cat=2,-20\'.\'&showposts=18\'.\'&paged=\'.$paged);
            ?>
            <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    </div>
        <div class="longLoopNav">
          <div class="alignleft"><?php previous_posts_link(\'&laquo; Previous\') ?></div>
          <div class="alignright"><?php next_posts_link(\'More &raquo;\') ?></div>
        </div>
        <?php $wp_query = null; $wp_query = $temp;?>
        <?php remove_filter(\'post_limits\', \'my_post_limit\'); ?>
现在要了解如何使用Ajax无限滚动地将更多帖子加载到此循环中

1 个回复
SO网友:Vladimir

为什么不能编写自定义分页?

function theme_get_next_posts_link($query, $label = null, $max_page = 0)    
    {       
        global $paged;      
        if(!$max_page) $max_page = $query->max_num_pages;       
        if(!$paged) $paged = 1;     
        $nextpage = intval($paged) + 1;     
        if(null === $label) $label = "Older Posts";     
        if(!is_single() && $nextpage <= $max_page):         
            $attr = apply_filters( \'next_posts_link_attributes\', \'\' );          
            echo \'<a class="page-l" href="\' . next_posts( $max_page, false ) . "\\" $attr>" . preg_replace(\'/&([^#])(?![a-z]{1,8};)/i\', \'&$1\', $label) . \'</a>\';     
        endif;  
    }       

    function theme_get_previous_posts_link($label = null)   
    {       
        global $paged;      
        if(null === $label) $label = "Newer Posts";     
        if(!is_single() && $paged > 1):         
            $attr = apply_filters( \'previous_posts_link_attributes\', \'\' );          
            echo \'<a class="page-r" href="\' . previous_posts( false ) . "\\" $attr>". preg_replace( \'/&([^#])(?![a-z]{1,8};)/\', \'&$1\', $label ) .\'</a>\';     
        endif;  
    }
使用:<?php theme_get_next_posts_link($first_query); ?> 对于较旧的帖子<?php theme_get_previous_posts_link(); ?> 对于较新的帖子

在代码中:

<?php
    global $paged;
    $args = array(
        \'post_type\'=>\'post\',
        \'post_status\'=> \'publish\',
        \'paged\'=> $paged,
        \'ignore_sticky_posts\'=> 1,
        \'posts_per_page\'=>10 // -1 - all
    );
    $fp_query = new WP_Query( $args );
    if ( $fp_query->have_posts() ):
    while ($fp_query->have_posts()) : $fp_query->the_post();
?>
    Posts
<?php endwhile;?>
    <div class="longLoopNav">
        <div class="alignleft"><?php theme_get_next_posts_link($fp_query); ?></div>
        <div class="alignright"><?php theme_get_previous_posts_link(); ?></div>
    </div>
<?php endif; ?>

结束

相关推荐

Pagination for a category

嗨,我有一个页面显示20个特定类别的特色图像和信息,我希望能够有分页,这样我就可以有多个类别的页面。我该怎么做?我似乎无法让它工作。任何帮助都将不胜感激谢谢<?php get_header(); ?> <div class=\"modus-grid\"> <?php query_posts(array(\'category__in\' => array(5), \'posts_per_page\' => 20)); ?>