我想将分页应用于我的自定义帖子类型。这是我使用的分页功能。
function roots_pagination($pages = '', $range = 2)
{
$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=\"pagination\">";
echo "<ul><li><span>Page ".$paged." of ".$pages."</span></li>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<li><a href='".get_pagenum_link(1)."'>« First</a></li>";
if($paged > 1 && $showitems < $pages) echo "<li><a href='".get_pagenum_link($paged - 1)."'>‹ Previous</a></li>";
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? "<li><a class=\"current\" href=\"#\">".$i."</a></li>":"<li><a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a></li>";
}
}
if ($paged < $pages && $showitems < $pages) echo "<li><a href=\"".get_pagenum_link($paged + 1)."\">Next ›</a></li>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<li><a href='".get_pagenum_link($pages)."'>Last »</a></li>";
echo "</ul></div>\n";
}
}
这是我用来调用分页的代码。
$paged = (get_query_var('paged'))?get_query_var('paged'):1;
//print_r($_SESSION);
//$paged = $_SESSION['paged'];
$temp=$wp_query;
$wp_query=null;
$wp_query=new Wp_Query();
$wp_query->query(array(
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'news',
'post_status' => 'publish',
'posts_per_page' => 4,
'paged'=>$paged
));
global $wp_query;
//$temp=$wp_query;
$wp_query->in_the_loop=true;
while ($wp_query->have_posts() ) : the_post();
//if($post_title !="27 Annual Convocation & Students Recognition Day"):
$title = get_the_title();
?>
<h4 style="border-top: medium double #dedcd2;">
<?php the_title();//echo $single_news->post_title; ?>
</h4>
<div class="text_exposed_root text_exposed" id="id_5485361e9d3362c15508684">
<p>
<?php echo substr(get_the_content(), 0, 250)."..."; //$single_news->post_content ?>
<a href="<?php the_permalink(); ?>"> View Details</a>
</p>
</div>
<?php
endwhile;
roots_pagination();
?>
当我运行此代码时,无论是否点击页面链接,它始终显示第一页。它可以很好地创建页码,但点击链接时它只显示第一页。请帮忙。