使用传统方式实现最终的outlook有点棘手。假设两个类别都至少有6篇文章。现在,首先使用get_posts()
不<罢工>WP_Query
罢工>
$side_news = get_posts(array(
\'post_type\' => \'post\',
\'category_name\' => \'category-slug-1\',
\'posts_per_page\' => 6
));
$side_news2 = get_posts(array(
\'post_type\' => \'post\',
\'category_name\' => \'category-slug-2\',
\'posts_per_page\' => 6
));
now that we know we have 6 post each let\'s merge the result as alternative position.
$c_result = array();
while(!empty($side_news) || !empty($side_news2)) {
if(!empty($side_news)) {
$result[] = array_shift($side_news);
}
if(!empty($side_news2)) {
$result[] = array_shift($side_news2);
}
}
Now we have an array with 12 posts. Let\'s loop through it and achieve the final layout:
echo \'<div class="row m-0">\';
$i = 0;
foreach ($c_result as $post) {
$i++;
$id = $post->ID;
$title = $post->post_title;
$content = $post->post_content;
if($i%2 == 0):
echo"<div class=\'col-8\'>$id $title $content</div>";
else:
echo"<div class=\'col-4\'>$id $title $content</div>";
endif;
}
echo \'</div>\';
get_posts()
returns post as object with following keys:
WP_Post Object
(
[ID] =>
[post_author] =>
[post_date] =>
[post_date_gmt] =>
[post_content] =>
[post_title] =>
[post_excerpt] =>
[post_status] =>
[comment_status] =>
[ping_status] =>
[post_password] =>
[post_name] =>
[to_ping] =>
[pinged] =>
[post_modified] =>
[post_modified_gmt] =>
[post_content_filtered] =>
[post_parent] =>
[guid] =>
[menu_order] =>
[post_type] =>
[post_mime_type] =>
[comment_count] =>
[filter] =>
)