我从“高级”Wordpress主题中获取了以下分页代码,试图将其集成到自定义主题中,但似乎无法使其正常工作:
在博客中。php页面模板,我有循环:
$paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
$args=array(
\'paged\' => $paged,
\'post_type\' => \'post\',
\'posts_per_page\' => \'1\',
\'order\' => \'DESC\',
\'orderby\' => \'date\',
);
$wp_query = new WP_query($args);
if ( $wp_query -> have_posts() ) :
$j = 1;
while ($wp_query -> have_posts()): $wp_query -> the_post();?>
CONTENT HERE
<?php endwhile;prestigo_SPaginate();endif;?>
其功能是:
if(!function_exists(\'prestigo_SPaginate\')){
function prestigo_SPaginate()
{
$currentPage = null;
$totalPage = null;
global $wp_query;
$currentPage = intval(get_query_var(\'paged\'));
if(empty($currentPage))
{
$currentPage = 1;
}
$totalPage = intval(ceil($wp_query->found_posts / intval(get_query_var(\'posts_per_page\'))));
if($totalPage <= 1)
{
return \'\';
}
$paginateResult = \'<!-- PAGINATION -->
<ul class="pagination clearfix">\';
if ($currentPage > 1)
{
$paginateResult .= \'<li class="pull-left"><a href="\'.get_pagenum_link($currentPage - 1).\'"><i class="fe arrow_carrot-left"></i></a></li>\';
}elseif ($currentPage = 1)
{
$paginateResult .= \'<li class="pull-left"><a href="javascript:void(0);"><i class="fe arrow_carrot-left"></i></a></li>\';
}
$paginateResult .= prestigo_ListLink(1, $totalPage, $currentPage);
if ($currentPage < $totalPage)
{
$paginateResult .= "<li class=\'pull-right\'><a href=\'" . get_pagenum_link($currentPage + 1) . "\' class=\'spaginate-next\'><i class=\'fe arrow_carrot-right\'></i></a></li>";
}elseif($currentPage = $totalPage){
$paginateResult .= "<li class=\'pull-right\'><span class=\'spaginate-next\'><i class=\'fe arrow_carrot-right\'></i></span></li>";
}
$paginateResult .= "</ul><!-- //PAGINATION -->";
echo $paginateResult;
return $paginateResult;
}
}
if(!function_exists(\'prestigo_ListLink\')){
function prestigo_ListLink($intStart, $totalPage, $currentPage)
{
$pageHidden = \'<span class="spaginate-hidden">... </span>\';
$linkResult = "";
$hiddenBefore = false;
$hiddenAfter = false;
for ($i = $intStart; $i <= $totalPage; $i++)
{
if($currentPage === intval($i))
{
$linkResult .= \'<li class="active"><a href="\'.get_pagenum_link($i).\'">\'.$i.\'</a></li>\';
}
else if(($i <= 6 && $currentPage < 10) || $i == $totalPage || $i == 1 || $i < 4 || ($i <= 6 && $totalPage <= 6) || ($i > $currentPage && ($i <= ($currentPage + 2))) || ($i < $currentPage && ($i >= ($currentPage - 2))) || ($i >= ($totalPage - 2) && $i < $totalPage))
{
$linkResult .= \'<li><a href="\'.get_pagenum_link($i).\'">\'.$i.\'</a></li>\';
if($i <= 6 && $currentPage < 10)
{
$hiddenBefore = true;
}
}
else
{
if(!$hiddenBefore)
{
$linkResult .= $pageHidden;
$hiddenBefore = true;
}
else if(!$hiddenAfter && $i > $currentPage)
{
$linkResult .= $pageHidden;
$hiddenAfter = true;
}
}
}
return $linkResult;
}
}
不幸的是,分页显示了出来,但单击第2页显示的帖子与第1页显示的帖子相同。
任何建议都很好,谢谢。
最合适的回答,由SO网友:Adrian 整理而成
对于这些天的分页,我使用以下内容:
function wpbeginner_numeric_posts_nav() {
if( is_singular() )
return;
global $wp_query;
/** Stop execution if there\'s only 1 page */
if( $wp_query->max_num_pages <= 1 )
return;
$paged = get_query_var( \'paged\' ) ? absint( get_query_var( \'paged\' ) ) : 1;
$max = intval( $wp_query->max_num_pages );
/** Add current page to the array */
if ( $paged >= 1 )
$links[] = $paged;
/** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
$links[] = $paged - 1;
$links[] = $paged - 2;
}
if ( ( $paged + 2 ) <= $max ) {
$links[] = $paged + 2;
$links[] = $paged + 1;
}
echo \'<div class="navigation"><ul>\' . "\\n";
/** Previous Post Link */
if ( get_previous_posts_link() )
printf( \'<li>%s</li>\' . "\\n", get_previous_posts_link() );
/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? \' class="active"\' : \'\';
printf( \'<li%s><a href="%s">%s</a></li>\' . "\\n", $class, esc_url( get_pagenum_link( 1 ) ), \'1\' );
if ( ! in_array( 2, $links ) )
echo \'<li>…</li>\';
}
/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? \' class="active"\' : \'\';
printf( \'<li%s><a href="%s">%s</a></li>\' . "\\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
}
/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) )
echo \'<li>…</li>\' . "\\n";
$class = $paged == $max ? \' class="active"\' : \'\';
printf( \'<li%s><a href="%s">%s</a></li>\' . "\\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}
/** Next Post Link */
if ( get_next_posts_link() )
printf( \'<li>%s</li>\' . "\\n", get_next_posts_link() );
echo \'</ul></div>\' . "\\n";
}