1 回答

TA貢獻(xiàn)1873條經(jīng)驗(yàn) 獲得超9個贊
我看到您的代碼有一些問題。首先在你的第一個如果
if ( $the_query->have_posts() )
$the_query 仍未定義,因此它沒有帖子,您將 $the_query 下面的幾行代碼定義為 WP_Query 類的實(shí)例。
其次,您wp_reset_postdata()有條件地調(diào)用,這意味著如果查詢沒有帖子發(fā)布數(shù)據(jù)將不會被重置。
當(dāng)然,您查詢數(shù)據(jù)庫的次數(shù)太多了,我不明白您為什么需要它。
我做了一些快速更正(注意下面的代碼未經(jīng)測試,我只是應(yīng)用了我能想到的快速解決方案)
<?php
$the_query = new WP_Query(
array(
'category_name' => 'Travel',
'posts_per_page' => 64,
)
);
?>
<div class="row row__padding--bottom">
<?php
if ( $the_query->have_posts() ) :
$i = 0;
$class_map = [
0 => 'col-md-6',
1 => 'col-md-6',
6 => 'col-md-6',
7 => 'col-md-6',
2 => 'col-md-4',
3 => 'col-md-4',
4 => 'col-md-4',
5 => 'col-md-12',
];
while ( $the_query->have_posts() ) :
$the_query->the_post();
?>
<div class="col-sm-12 <?php echo esc_attr( $class_map[ $i % 8 ] ); ?>">
<div class="journal__featured" style="background: url(<?php the_post_thumbnail_url( 'large' ); ?>) !important; !important; background-size: cover !important; background-position: center center !important; background-repeat: no-repeat !important;">
</div>
<div class="post__info--container">
<a href="<?php the_permalink(); ?>"><h4><?php the_title(); ?></h4></a>
</div>
</div>
<?php
if ( 1 === ($i % 8) || 4 === ($i % 8) || 5 === ($i % 8) ) {
echo '</div><div class="row row__padding--bottom">';
}
$i++;
endwhile;
else :
echo '<p>' . esc_html( __( 'No News' ) ) . '</p>';
endif;
wp_reset_postdata();
?>
</div>
<?php
希望這對你有用!
- 1 回答
- 0 關(guān)注
- 193 瀏覽
添加回答
舉報(bào)