【发布时间】:2026-01-27 19:35:02
【问题描述】:
当我们在同一页面中有多个循环时,最好的解决方案是什么?我正在使用这个主循环:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php endwhile; endif; ?>
现在我正在尝试在同一页面(在不同位置)为特定类别名称中的精选帖子添加一个新循环,这是您的最佳选择:(“内容”只是示例)
1- 使用 get_posts();
<?php global $post;
$args = array( 'category_name' => 'destaques' );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
"content"
<?php endforeach; ?>
2- 使用 WP_Query();
<php $my_query = new WP_Query("category_name=destaques");
while ($my_query->have_posts()) : $my_query->the_post(); ?>
"content"
<?php endwhile; ?>
3:使用 query_posts();
<?php query_posts( 'category_name=destaques' );
if (have_posts()) : while (have_posts()) : the_post(); ?>
"content"
<?php endwhile; endif; ?>
您选择哪个以及为什么?
谢谢。
【问题讨论】:
标签: themes wordpress-theming wordpress