我有一个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' );
}
在and grid.php中,我使用了一个WP_Query和一个分页。分页显示为OK。当我单击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' );
}
我不确定是不是这样,但如果是这样,我不知道如何让它工作。
发布于 2018-01-10 23:20:21
尝试在您的主题文件夹中创建:
适用于网格
page.create-album.php
的
page.create-album.php
与inside <?php //Template Name: Create Album
//Here all the html and php
带有inside 的
page.my-account.php
<?php //Template Name: My Account
//Here all the html and 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' );
}
我不知道它是否能帮你解决这个问题,但我认为它会更干净,而且我已经用这种方式在home.php上做了分页操作,没有任何问题。
试一试,如果它不起作用,我们会找到另一种方法:)
https://stackoverflow.com/questions/48187165
复制相似问题