我在Wordpress的第一天,一切都不顺利。。。我只是有一个简单的循环,我希望在其中显示特定类别的一些帖子。这是我的代码:
<?php
$args = array(
\'numberposts\' => 5,
\'category\' => 2,
\'offset\' => 0,
\'order\' => \'date\',
\'post_type\' => \'post\',
\'post_status\' => \'publish\'
);
$foodPosts = get_posts($args);
foreach ($foodPosts as $foodPost) : setup_postdata($foodPost);
?>
<div><?php the_permalink(); ?></div>
<div><?php the_title(); ?></div>
<div><?php the_content(); ?></div>
<?php endforeach; wp_reset_postdata(); ?>
这是输出:
http://10.0.1.174/mysite/?p=7
Cool post title
this is yet another food post
http://10.0.1.174/mysite/?p=7
Cool post title
this post is in the food category
http://10.0.1.174/mysite/?p=7
Cool post title
this is a post about food
因此,每个帖子的内容都是正确的,但由于某种原因,所有的永久链接和标题都与第一篇帖子相同。怎么了?
最合适的回答,由SO网友:Abdul Awal Uzzal 整理而成
尝试使用WP_Query
相反因此,代码如下所示:
<?php
$args = array(
\'posts_per_page\' => 5,
\'cat\' => 2,
\'offset\' => 0,
\'order\' => \'date\',
\'post_type\' => \'post\',
\'post_status\' => \'publish\'
);
$post_query = new WP_Query($args);
if($post_query->have_posts()) : while($post_query->have_posts()) : $post_query->the_post();
?>
<div><?php the_permalink(); ?></div>
<div><?php the_title(); ?></div>
<div><?php the_content(); ?></div>
<?php
endwhile;
endif;
wp_reset_postdata();
?>