自定义帖子类型的分页问题

时间:2011-11-30 作者:toomanyairmiles

此处自定义帖子类型的分页链接失败http://mjw.view-wireframes.com/about-us/press/

页面将重新加载http://mjw.view-wireframes.com/about-us/press/page/2/ 但这对显示的帖子没有影响。刷新permalink结构没有效果,所以我猜问题出在查询中。

<?php 
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 10;
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query(array( \'post_type\' => \'press\',
                                \'orderby\'   => \'post_date\',
                                \'posts_per_page\' => $paged,
                                ));
while ($wp_query->have_posts()) : $wp_query->the_post(); 



    // The following determines what the post format is and shows the correct file accordingly
    $format = get_post_format();
    get_template_part( \'/lib/includes/post-formats/\'.$format );

    if($format == \'\')
    get_template_part( \'/lib/includes/post-formats/standard\' );

endwhile;


global $wp_query;

$big = 999999999; // need an unlikely integer

echo paginate_links( array(
    \'base\' => str_replace( $big, \'%#%\', get_pagenum_link( $big ) ),
    \'format\' => \'?paged=%#%\',
    \'current\' => max( 1, get_query_var(\'paged\') ),
    \'total\' => $wp_query->max_num_pages
) );

$wp_query = null; $wp_query = $temp; ?> 
分页功能

<?php $wp_query = null; $wp_query = $temp;?> 

function pagination($pages = \'\', $range = 4)
{
     $showitems = ($range * 2)+1;  

     global $paged;
     if(empty($paged)) $paged = 1;

     if($pages == \'\')
     {
         global $wp_query;
         $pages = $wp_query->max_num_pages;
         if(!$pages)
         {
             $pages = 1;
         }
     }   

     if(1 != $pages)
     {
         echo "<div class=\\"theme-pagination\\"><ul><li><span>Page ".$paged." of ".$pages."</span></li>";
         if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<li><span><a href=\'".get_pagenum_link(1)."\'>&laquo; First</a></span></li>";
         if($paged > 1 && $showitems < $pages) echo "<li><span><a href=\'".get_pagenum_link($paged - 1)."\'>&lsaquo; Previous</a></span></li>";

         for ($i=1; $i <= $pages; $i++)
         {
             if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
             {
                 echo ($paged == $i)? "<li><span class=\\"current\\">".$i."</span></li>":"<li><span><a href=\'".get_pagenum_link($i)."\' class=\\"inactive\\">".$i."</a></span></li>";
             }
         }

         if ($paged < $pages && $showitems < $pages) echo "<li><span><a href=\\"".get_pagenum_link($paged + 1)."\\">Next &rsaquo;</a></span></li>";
         if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<li><span><a href=\'".get_pagenum_link($pages)."\'>Last &raquo;</a></span></li>";
         echo "</ul></div>\\n";
     }
}

2 个回复
最合适的回答,由SO网友:toomanyairmiles 整理而成

答案是使用orderby date而不是post date!

SO网友:Chip Bennett

存储您的new WP_Query() 在变量中,全球化$wp_query$wp_query 在您的new WP_Query() 生成将变量传递给$wp_query, new WP_Query() 调用自身例如。

<?php 
// Custom query args
$press_query_args = array(
    \'post_type\' => \'press\',
    \'orderby\'   => \'post_date\',
    //\'showposts\' => \'10\',
    \'posts_per_page\' => $paged,
);
// Custom query
$press_query = new WP_Query( $press_query_args );

// Globalize $wp_query
global $wp_query;
// Swap-hack
$temp = $wp_query;
$wp_query= null;
$wp_query = $press_query;

// Output custom loop
while ($press_query->have_posts()) : $press_query->the_post(); 

// EVERYTHING FROM HERE BELOW SHOULD BE THE SAME,
// THOUGH I\'VE CLEANED IT UP A BIT
// (MAINLY, REMOVING THE CLOSE/OPEN PHP TAG COMBOS)

// The following determines what the post format is and shows the correct file accordingly
$format = get_post_format();
get_template_part( \'/lib/includes/post-formats/\'.$format );

if($format == \'\')
get_template_part( \'/lib/includes/post-formats/standard\' );

endwhile;  

if (function_exists("pagination")) {pagination($additional_loop->max_num_pages);} 

$wp_query = null; $wp_query = $temp;
?> 

EDIT

回复:每页的分页/张贴问题:

Hi chip,谢谢你,但这会产生一些奇怪的行为,在第一页你会看到10篇文章,然后从第1-7页分页,但在第二页你会看到第一页上出现的前两篇文章,分页编号从2-35变为2-35

我怀疑问题出在这里:

$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 10;
与此数组参数结合使用:

$wp_query->query(array( 
    \'posts_per_page\' => $paged,
));

What happens if you remove $paged and the \'posts_per_page\' array argument?

此外:

分页(1-7 vs 2-35)问题可能与每页文章问题(尽管可能相关)是分开的。尝试使用previous_posts_link()next_posts_link(), 只是为了隔离每页的帖子。

结束

相关推荐

Authors list Pagination?

WordPress有一个内置功能,可以显示网站所有作者的列表。但是没有显示他们的头像的选项,所以如果你有作者,你真正得到的只是一个链接到作者页面的文本列表。php文件在您的主题中,即。因此,打开互联网,我找到了这本不错的教程bavotasan.com 用一小段代码似乎就能做到这一点。在我的网站上,所有用户都可以写文章,贡献者的名单很长。有可能设定10 users for page ? 使用此解决方案:将结果集分页自$wpdb->get_results()我确实为作者编写了如下代码列表函数: f