【发布时间】:2018-12-04 07:41:16
【问题描述】:
我有一个 index.php 页面,我在其中添加了以下代码,以根据加载的页面显示不同的内容:
if( is_page( 'my-account' ) ) {
get_template_part( 'template-parts/my-account', 'account' );
} else if( is_home() || is_front_page() ) {
get_template_part( 'template-parts/album-grid', 'index' );
} else if( is_page( 'create-album') ) {
get_template_part( 'template-parts/create-album', 'create-album' );
}
在 album-grid.php 中,我使用的是 WP_Query 和分页。分页显示确定。当我点击 Next 链接时,它会将我带到http://albumlocal.com/page/2,但没有显示任何内容。我期待在这里看到下一组记录。
这是我在album-grid.php 文件上的代码:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$posts = new WP_Query( array(
'taxonomy' => 'album-category',
'post_type' => array('albums'),
'posts_per_page' => 10,
'paged' => $paged,
)
);
if( $posts->have_posts() ) { ?>
<div class="grid-table" xmlns="http://www.w3.org/1999/html">
<?php
while ( $posts->have_posts() ) :$posts->the_post(); ?>
<a class="grid-anchor" href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<div class="grid-col">
<h3><?php the_title(); ?></h3>
<div class="grid-thumbnail">
<?php the_post_thumbnail('home_grid_thumbnail'); ?>
</div>
<div class="grid-action">
<i class="fa fa-camera"></i> Photos
</div>
</div>
</a>
<?php
endwhile;
wp_reset_query(); ?>
</div>
<?php } ?>
<div class="clear"></div>
<?php echo paginate_links( array(
'total' => $posts->max_num_pages,
'mid_size' => 2,
'format' => 'page/%#%'
)); ?>
我认为当我在第二个页面或任何子页面(例如 http://albumlocal.com/page/2)is_home() 或 is_front_page() 时不再满足,因此它不再加载所需的模板部分 album-grid.php。
if( is_home() || is_front_page() ) {
get_template_part( 'template-parts/album-grid', 'index' );
}
我不确定是不是这样,但如果是这样,我不知道如何让它工作。
【问题讨论】:
-
我真的不明白你想做什么?您的主页上有分页吗?
-
不,分页在模板部分
album-grid.php内。
标签: wordpress pagination