【问题标题】:is_home() or is_front_page() check with pagination does not show content of next pageis_home() 或 is_front_page() 检查分页不显示下一页的内容
【发布时间】: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/2is_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


【解决方案1】:

尝试在您的主题文件夹中创建:

  • 用于网格的 home.php
  • 页面模板page.create-album.phpwith inside

<?php //Template Name: Create Album
//Here all the html and php

  • 页面模板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 进行了分页,没有任何问题。

试一试,如果不行,我们会找到另一种方法:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-30
    • 2012-10-17
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多