Php WordPress循环,分别列出标题和内容

Php WordPress循环,分别列出标题和内容,php,html,loops,wordpress,Php,Html,Loops,Wordpress,我是wordpress的新手,我想创建一个循环,在左分区中列出帖子标题(仅来自一个类别),在右分区中列出相同帖子的内容。这就是我所做的,我想知道是否有更好的方法来做到这一点(嗯,可能有)?谢谢 这不好,同一代码运行两次,您可以在一个循环中使用get\u title()、get\u post()、get\u content(),它们返回字符串而不是立即输出字符串。使用get\u posts代替: $output1=''; $output2=''; $args=数组('cat'=>6); $al

我是wordpress的新手,我想创建一个循环,在左分区中列出帖子标题(仅来自一个类别),在右分区中列出相同帖子的内容。这就是我所做的,我想知道是否有更好的方法来做到这一点(嗯,可能有)?谢谢



这不好,同一代码运行两次,您可以在一个循环中使用get\u title()、get\u post()、get\u content(),它们返回字符串而不是立即输出字符串。

使用get\u posts代替:

$output1='';
$output2='';
$args=数组('cat'=>6);
$allposts=get_posts($args);
foreach($allposts作为$post):
设置_postdata($post);
//将标题存储在一个变量中
$output1.=''。获取标题();
//以及另一个版本中的内容
$output2.=wpautop(获取内容());
endforeach;
wp_reset_query();

然后在需要它们的地方回显$output1和$output2——如果需要更多控制,甚至可以将它们存储为数组。您可以向$args数组中添加参数,以根据需要更改顺序等。

永远不要使用
query\u posts
。它很慢,会中断主查询,并中断页面功能。您可以在一个查询中完成所有操作。几个月前我做过类似的事情,虽然我不记得在哪里。谢谢!问题是,在一个循环中,我按这个顺序得到一个结果——标题,内容,标题,内容,我需要标题,标题,内容,内容。我需要列表,一个列出标题,另一个列出内容谢谢!这很有帮助!很高兴能帮上忙-如果你需要进一步的帮助,请告诉我。学习get_posts功能将对WordPress的开发有很大帮助。WordPress codex是您的朋友:)
<div class="container">
<div class="post_title">
    <?php query_posts('cat=6'); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <h3><?php the_title(); ?></h3>
    <?php endwhile; endif; ?>
    <?php rewind_posts(); ?> 
</div>          
<div class="post_content">
    <?php query_posts('cat=6'); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php the_content(); ?>
    <?php endwhile; endif; ?>
    <?php wp_reset_query(); ?>
</div>                                            
$output1 = '';
$output2 = '';
$args = array('cat'=> 6);
$allposts = get_posts( $args );
foreach ( $allposts as $post ) :
  setup_postdata( $post );
  // Store the titles in one variable
  $output1 .= '<h3>'.get_the_title().'</h3>';
  // And the content in another
  $output2 .= wpautop(get_the_content());
endforeach;
wp_reset_query();