因此,我花了一天的大部分时间才让我的查询正常工作(有时我觉得我根本不知道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( __( \'← Older posts\', \'foundationpress\' ) ); ?></div>
<div class="post-next"><?php previous_posts_link( __( \'Newer posts →\', \'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( __( \'← Older posts\', \'foundationpress\' ) ); ?></div>
<div class="post-next"><?php previous_posts_link( __( \'Newer posts →\', \'foundationpress\' ) ); ?></div>
</nav>
<?php } ?>