多个 Wordpress 循环不同的类别

Multiple Wordpress Loops different categories

我真的很困惑它是如何工作的,我查看了 Wordpress 文档,但无论如何我都找不到任何简单的东西。我的主索引页面需要 3 个循环,每个循环都基于一个类别,并且只需要从该类别中获取最新的 post。

我已经让它工作得很好,但我只是想知道这样做是否正确?显然它有效,但这样做会给我带来任何问题吗?有正确的做法吗?

//loop 1

<div class="large-4 columns">

<?php query_posts( 'category_name=stories&posts_per_page=1' ); ?>

<?php while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>

</div>

//loop 2

<div class="large-4 columns">

<?php query_posts( 'category_name=pictures&posts_per_page=1' ); ?>

<?php while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>

</div>

//loop 3

<div class="large-4 columns">

<?php query_posts( 'category_name=videos&posts_per_page=1' ); ?>

<?php while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>

</div>

忽略HTML,因为我还没有格式化它。有什么帮助吗?谢谢

是的,这可能存在问题。您正在修改原始的 wordpress 查询。您应该忽略使用 query_posts。你最好使用以下之一。

1.) get_posts ref

2.) 自定义 wp 查询 ref

改变

<?php query_posts( 'category_name=stories&posts_per_page=1' ); ?>

<?php while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>

</div>

<div class="large-4 columns">
<?php
$the_query = new WP_Query( 'category_name=stories&posts_per_page=1' );
while ( $the_query->have_posts() ) {
    $the_query->the_post();
    the_title();
    the_content();
}
/* Restore original Post Data */
wp_reset_postdata();
/* Added */
?>
</div>