我有一个正常的循环,根据给定的$ args输出帖子。 在三篇帖子之后,我想插入一个来自精选类别的帖子。我尝试过以不同的组合方式启动新的WP_Query,简单的query_posts。似乎没什么用。有什么想法吗?
$args = array(
'post_type' => 'post',
'posts_per_page' => $count,
'paged' => $paged,
'page' => $paged,
'cat' => $cat,
'ignore_sticky_posts' => 1
);
// create a new instance of WP_Query
$my_query = new WP_Query($args);
<ul>
<?php
$i = 0;
if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post();
if($i == 3): ?>
<li> //insert here one post from featured category
</li>
<?php endif; ?>
<li>
// the normal query stuff is here
</li>
<?php $i++ //post counter
endwhile; //end loop while
endif; //end loop
?>
</ul>
答案 0 :(得分:0)
$args2 = array(
'post_type' => 'post',
'posts_per_page' => 1,
'category_name' => 'TheCategoryName-Slug',
'ignore_sticky_posts' => 1
);
$my_query2 = new WP_Query($args2);
$post_ids = array(); //create an array to store the ids of the posts
if ($my_query2->have_posts()) : while ($my_query2->have_posts()) : $my_query2->the_post();
$post_ids[] = get_the_ID();
endwhile;
wp_reset_postdata();
endif;
拥有所有ID后,您可以使用它们来获取所需的数据。基本上,来自wp_posts表(http://codex.wordpress.org/Database_Description#Table:_wp_posts)的所有数据。帖子作者返回为id,可以使用get_userdata()函数获取名称。
echo get_post_field('post_title', $post_ids[0]);
echo get_post_field('post_author', $post_ids[0]);
echo get_post_field('post_date', $post_ids[0]);