[英]WP_Query: How to test for sticky posts before assigning posts_per_page?
[英]Custom WP_Query 'posts_per_page' is not working
当我声明“posts_per_page”时,我遇到了显示多个帖子的问题。
在我的研究过程中,我的代码已经开发,但仍然没有结果。 我试过了
'ignore_sticky_posts' => 1
, 'nopaging' => true
(分页)
切换主题和停用插件。 我似乎找不到问题所在。 任何帮助将非常感激。 这是当前结果 - alderneyfootball.co.uk
我在页面上有两个循环,它是一个自定义页面模板。 我正在使用WP-Club-Manager 插件
<?php
// the query
wp_reset_query();
$wpb_next_game = array('post_type'=>'wpcm_match', 'post_status' => 'future', 's' => 'Alderney', 'posts_per_page' => 1 );
$wpb_next_game = new WP_Query($wpb_next_game);
if ( have_posts() ) : while ( $wpb_next_game->have_posts() ) : $wpb_next_game->the_post();
$date = date_i18n( get_option( 'date_format' ), strtotime( $post->post_date ) );
$time = date_i18n( get_option( 'time_format' ), strtotime( $post->post_date ) );
$side = wpcm_get_match_clubs( $post->ID );
// badge
$badges = wpcm_get_match_badges( $post->ID, 'crest-medium', array( 'class' => 'home-logo' ) );
$badges = wpcm_get_match_badges( $post->ID, 'crest-medium', array( 'class' => 'away-logo' ) );
$format = get_match_title_format();
if( $format == '%home% vs %away%') {
$badge = $badges[0];
} else {
$badge = $badges[1];
}
?>
<div id="next-match" class="row">
<div class="span_6 col">
<h3 class="next-match-home"><?php echo $side[0]; ?></h3>
</div>
<div class="span_6 col">
<h3 class="next-match-away"><?php echo $side[1]; ?></h3>
</div>
</div>
<div class="row">
<h4 id="next-match-time">Next Match at <?php echo $time; ?> on <?php echo $date; ?></h4>
<?php endwhile; ?>
<!-- end of the loop -->
<?php else : ?>
<h4 id="next-match-time"><?php _e( 'Sorry, scheduled matches' ); ?></h4>
<?php endif; ?>
如果两个循环数据被覆盖,那么我的第一个代码 wp_reset_query() 是不正确的。 如果您使用的是 WP_Query 那么
wp_reset_postdata() //remove wp_reset_query() which is used for wp_query()
应该在 WHILE 循环结束后使用,这意味着在你的两个循环中你必须有
wp_reset_postdata() // use this at both loops
在 while 循环末尾的两个循环中。
现在你的代码看起来像这样:
<?php
$wpb_next_game = array('post_type'=>'wpcm_match', 'post_status' => 'future', 's' => 'Alderney', 'posts_per_page' => 1 );
$wpb_next_game = new WP_Query($wpb_next_game);
if ( have_posts() ) : while ( $wpb_next_game->have_posts() ) : $wpb_next_game->the_post();
$date = date_i18n( get_option( 'date_format' ), strtotime( $post->post_date ) );
$time = date_i18n( get_option( 'time_format' ), strtotime( $post->post_date ) );
$side = wpcm_get_match_clubs( $post->ID );
// badge
$badges = wpcm_get_match_badges( $post->ID, 'crest-medium', array( 'class' => 'home-logo' ) );
$badges = wpcm_get_match_badges( $post->ID, 'crest-medium', array( 'class' => 'away-logo' ) );
$format = get_match_title_format();
if( $format == '%home% vs %away%') {
$badge = $badges[0];
} else {
$badge = $badges[1];
}
?>
<div id="next-match" class="row">
<div class="span_6 col">
<h3 class="next-match-home"><?php echo $side[0]; ?></h3>
</div>
<div class="span_6 col">
<h3 class="next-match-away"><?php echo $side[1]; ?></h3>
</div>
</div>
<div class="row">
<h4 id="next-match-time">Next Match at <?php echo $time; ?> on <?php echo $date; ?></h4>
<?php endwhile;
wp_reset_postdata();
?>
<!-- end of the loop -->
<?php else : ?>
<h4 id="next-match-time"><?php _e( 'Sorry, scheduled matches' ); ?></h4>
<?php endif; ?>
希望这对你有用
谢谢你
也许您的下一个循环需要来自上一个的查询。 确保您对 query_posts 使用wp_reset_query()和/或对 wp_query 使用 wp_reset_postdata( )
在每个循环之后。
我面临着同样的问题。 如果您在代码中添加了下面的代码,请将其删除,它将为您工作。
wp_reset_postdata()
谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.