分页页面上未显示的内容

时间:2016-05-17 作者:Justice Is Cheap

因此,我花了一天的大部分时间才让我的查询正常工作(有时我觉得我根本不知道WP),然后我在分页页面上遇到了404错误,我已经修复了这个错误,但现在我要转到“第2页”,但循环似乎不起作用。

这是我的索引。php查询(从另一篇SE帖子拼凑而成):

<?php
$do_not_duplicate=0;
$paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1; 
if(!is_paged()):
  //Get featured content for page 1
  $pagecount = $my_query->max_num_pages;
  $my_query = new WP_Query(\'category_name=featured&posts_per_page=50\');
  while ($my_query->have_posts()) : $my_query->the_post();
       $do_not_duplicate = $post->ID;?>
    <div class="row event-row">
        <div class="training-meta small-12 large-3 columns">
            <?php 
                // get raw date
                $training_date = get_field(\'training_date\', false, false);
                // make date object
                $training_date = new DateTime($training_date);
            ?>
            <h4 class="training-meta-date"><?php echo $training_date->format(\'M d, Y\'); ?> <span>at <?php the_field(\'training_time\'); ?></span></h4>
            <hr />
            <?php the_field(\'training_location\'); ?>
            <a class="register-button" href="<?php echo the_field(\'registration_url\'); ?>">Register Now</a>
        </div><?php /*== //.training-meta ==*/ ?>
        <div class="training-details small-12 large-8 columns">
            <h3 class="event-title"><?php the_title(); ?></h3>
            <span class="inline-date"><?php echo $training_date->format(\'M d, Y\'); ?></span> <?php the_field(\'training_details\'); ?>
        </div><?php /*== //.training-details ==*/ ?>
    </div><?php /*== //.row .event-row ==*/ ?>

<?php endwhile; endif; ?>
    <div class="row">
        <div class="small-12 large-12 large-centered">
            <h3 class="page-entry-title upcoming">Past Training Sessions</h3>
        </div>
    </div>
<?php if (have_posts()) : while (have_posts()) : the_post(); if( $post->ID == $do_not_duplicate ) continue; ?>
    <div class="training-details small-12 large-6 large-centered">
        <h3 class="event-title"><?php the_title(); ?></h3>
        <span class="inline-date"><?php echo $training_date->format(\'M d, Y\'); ?></span> <?php the_field(\'training_details\'); ?>
    </div><?php /*== //.training-details ==*/ ?>
<?php endwhile; endif; ?>

    <?php /* Display navigation to next/previous pages when applicable */ ?>
    <?php if ( function_exists( \'foundationpress_pagination\' ) ) { foundationpress_pagination(); } else if ( is_paged() ) { ?>
        <nav id="post-nav">
            <div class="post-previous"><?php next_posts_link( __( \'&larr; Older posts\', \'foundationpress\' ) ); ?></div>
            <div class="post-next"><?php previous_posts_link( __( \'Newer posts &rarr;\', \'foundationpress\' ) ); ?></div>
        </nav>
    <?php } ?>
这是我的pre_get_posts 函数(如果没有此函数,分页将无法工作):

    function training_homepage_posts( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( \'post_type\', array( \'post\' ) );
    }
}
add_action( \'pre_get_posts\', \'training_homepage_posts\' );
这是my dev URL 以便查看发生的情况。基本上,当我分页时,我希望在第二页上显示相同的特色帖子,但在其下方显示过去的事件(尽管我很高兴分页显示标题之外的其他过去事件内容)。

如果能朝着正确的方向努力,我们将不胜感激。客户对我解决这个问题(项目的短期周转)感到不安,我也是。

TIA!

EDIT TO SHOW CORRECTED CODE:

<?php
$do_not_duplicate=0;
$paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1; 
  //Get featured content for page 1
  $pagecount = $my_query->max_num_pages;
  $my_query = new WP_Query(\'category_name=featured&posts_per_page=50\');
  while ($my_query->have_posts()) : $my_query->the_post();
       $do_not_duplicate = $post->ID;?>
    <div class="row event-row">
        <div class="training-meta small-12 large-3 columns">
            <?php 
                // get raw date
                $training_date = get_field(\'training_date\', false, false);
                // make date object
                $training_date = new DateTime($training_date);
            ?>
            <h4 class="training-meta-date"><?php echo $training_date->format(\'M d, Y\'); ?> <span>at <?php the_field(\'training_time\'); ?></span></h4>
            <hr />
            <?php the_field(\'training_location\'); ?>
            <a class="register-button" href="<?php echo the_field(\'registration_url\'); ?>">Register Now</a>
        </div><?php /*== //.training-meta ==*/ ?>
        <div class="training-details small-12 large-8 columns">
            <h3 class="event-title"><?php the_title(); ?></h3>
            <span class="inline-date"><?php echo $training_date->format(\'M d, Y\'); ?></span> <?php the_field(\'training_details\'); ?>
        </div><?php /*== //.training-details ==*/ ?>
    </div><?php /*== //.row .event-row ==*/ ?>

<?php endwhile; ?>
    <div class="row">
        <div class="small-12 large-12 large-centered">
            <h3 class="page-entry-title upcoming">Past Training Sessions</h3>
        </div>
    </div>
<?php if (have_posts()) : while (have_posts()) : the_post(); if( $post->ID == $do_not_duplicate ) continue; ?>
    <div class="training-details small-12 large-6 large-centered">
        <h3 class="event-title"><?php the_title(); ?></h3>
        <span class="inline-date"><?php echo $training_date->format(\'M d, Y\'); ?></span> <?php the_field(\'training_details\'); ?>
    </div><?php /*== //.training-details ==*/ ?>
<?php endwhile; endif; ?>

    <?php /* Display navigation to next/previous pages when applicable */ ?>
    <?php if ( function_exists( \'foundationpress_pagination\' ) ) { foundationpress_pagination(); } else if ( is_paged() ) { ?>
        <nav id="post-nav">
            <div class="post-previous"><?php next_posts_link( __( \'&larr; Older posts\', \'foundationpress\' ) ); ?></div>
            <div class="post-next"><?php previous_posts_link( __( \'Newer posts &rarr;\', \'foundationpress\' ) ); ?></div>
        </nav>
    <?php } ?>

1 个回复
最合适的回答,由SO网友:cjbj 整理而成

具有is_paged 您正在测试是否已使用QuickTag将帖子划分为多个部分。但使用next\\u posts\\u link,您将指向下一页的帖子。所以你可能需要再做一次测试。

相关推荐

Can't Add Pagination

我无法添加分页来在页面之间划分帖子并在页面之间导航。目前所有20篇帖子我都有一次加载。我想在我的页面底部添加分页,并在5之前查看帖子。我有一个自定义的帖子类型,我将其定义为Project。我有两页。主页和日志。主页以我的项目帖子类型为特征,日志以我的常规博客帖子为特征。我希望这两页都有分页。我的当前索引。php是我的主页,如下所示:<?php get_header(); ?> <?php get_footer(); ?> <div class=\"p