我知道这是一个重复的问题。但对于现有的问题,我的观点还不够。
所以我们开始。。。。
分页不适用于自定义主页模板内的自定义查询。
下面的解决方案(适用于许多其他问题)取自另一个问题。
How to fix pagination for custom loops?
<div class="most-popular type-1">
<h3 class="section-headline"><?php _e(\'Most Popular\',\'themename\'); ?></h3>
<div class="widget-articles clearfix">
<?php
$mostpopular_args=array(
\'post_type\' => \'post\',
\'orderby\' => \'meta_value_num\',
\'meta_key\' => \'view_count\',
\'posts_per_page\' => \'10\',
);
$mostpopular_args[\'paged\'] = get_query_var( \'paged\' ) ? get_query_var( \'paged\' ) : 1;
?>
<?php
$mostpopular_pick = new WP_Query($mostpopular_args);
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $mostpopular_pick;
?>
<?php if($mostpopular_pick->have_posts() ): ?>
<?php while($mostpopular_pick->have_posts()): $mostpopular_pick->the_post(); ?>
<article class="widget-article">
<a href ="<?php the_permalink(); ?>">
<div class="widget-article-thumb">
<?php if(has_post_thumbnail()){ ?>
<?php $page_thumb = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), \'featuredsmall\'); ?>
<img src="<?php echo $page_thumb[0]; ?>" alt="" />
<?php } ?>
</div>
</a>
<div class="widget-content-wrapper">
<h3 class="widget-article-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
</div>
<div class="only-count">
<div class="icon icon-share-alt" aria-hidden="true"></div>
<?php echo do_shortcode(\'[mashshare shares="true" buttons="false"]\'); ?>
<div class="share-text">SHARES</div>
</div>
</article>
<?php endwhile; ?>
<?php endif; ?>
<?php
wp_reset_postdata();
// Custom query loop pagination
previous_posts_link( \'Older Posts\' );
next_posts_link( \'Newer Posts\', $mostpopular_pick->max_num_pages );
// Reset main query object
$wp_query = NULL;
$wp_query = $temp_query;
?>
</div>
</div>
然而,这对我不起作用,我不知道为什么。是否有任何外部因素?
这是我的总自定义主页模板。
<?php
/*
Template Name: Home Page
*/
?>
<?php get_header(); ?>
<div class="main-content col-xs-12" role="main">
<div class="row">
<?php get_sidebar(\'abovethefold\'); ?>
</div>
<div class="row">
<?php get_sidebar(\'homecolumnone\'); ?>
<div class="homecolumntwo-sidebar col-sm-4 col-md-5">
<div class="most-popular type-1">
<h3 class="section-headline"><?php _e(\'Most Popular\',\'themename\'); ?></h3>
<div class="widget-articles clearfix">
<?php
$mostpopular_args=array(
\'post_type\' => \'post\',
\'orderby\' => \'meta_value_num\',
\'meta_key\' => \'view_count\',
\'posts_per_page\' => \'10\',
);
$mostpopular_args[\'paged\'] = get_query_var( \'paged\' ) ? get_query_var( \'paged\' ) : 1;
?>
<?php
$mostpopular_pick = new WP_Query($mostpopular_args);
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $mostpopular_pick;
?>
<?php if($mostpopular_pick->have_posts() ): ?>
<?php while($mostpopular_pick->have_posts()): $mostpopular_pick->the_post(); ?>
<article class="widget-article">
<a href ="<?php the_permalink(); ?>">
<div class="widget-article-thumb">
<?php if(has_post_thumbnail()){ ?>
<?php $page_thumb = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), \'featuredsmall\'); ?>
<img src="<?php echo $page_thumb[0]; ?>" alt="" />
<?php } ?>
</div>
</a>
<div class="widget-content-wrapper">
<h3 class="widget-article-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
</div>
<div class="only-count">
<div class="icon icon-share-alt" aria-hidden="true"></div>
<?php echo do_shortcode(\'[mashshare shares="true" buttons="false"]\'); ?>
<div class="share-text">SHARES</div>
</div>
</article>
<?php endwhile; ?>
<?php endif; ?>
<?php
wp_reset_postdata();
// Custom query loop pagination
previous_posts_link( \'Older Posts\' );
next_posts_link( \'Newer Posts\', $mostpopular_pick->max_num_pages );
// Reset main query object
$wp_query = NULL;
$wp_query = $temp_query;
?>
</div>
</div>
</div> <!-- end sidebar -->
<?php //get_sidebar(\'homecolumntwo\'); ?>
<?php get_sidebar(\'homecolumnthree\'); ?>
</div>
</div> <!-- end main-content -->
<?php get_footer(); ?>
任何帮助都将不胜感激:)谢谢<;3.
SO网友:jay.jivani
用于分页
<?php
$paged = ( get_query_var( \'paged\' ) ) ? absint( get_query_var( \'paged\' ) ) : 1;
$mostpopular_args=array(
\'post_type\' => \'post\',
\'orderby\' => \'meta_value_num\',
\'meta_key\' => \'view_count\',
\'paged\' => $paged,
\'posts_per_page\' => \'10\',
);
$wp_query = new WP_Query($mostpopular_args);
while ($wp_query->have_posts()): $wp_query->the_post();
get_the_title();
endwhile;
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
\'base\' => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
\'format\' => \'?paged=%#%\',
\'current\' => max( 1, get_query_var(\'paged\') ),
\'total\' => $wp_query->max_num_pages
) );
?>