这是我试图用wp_query实现的模式。 它将用于相同的帖子类型。用wp_query可以这样做吗?
谢谢!
<div>
<div>1st post from featured</div>
<div>1st post not from featured</div>
<div>2nd post from featured</div>
<div>3rd post from featured</div>
<div>2nd post not from featured</div>
<div>3rd post not from featured</div>
..and then the same pattern start again with the rest of the posts
<div>4th post from featured</div>
<div>4th post not from featured</div>
<div>5th post from featured</div>
<div>6th post from featured</div>
<div>5th post not from featured</div>
<div>6th post not from featured</div>
..until there's no more post
</div>
答案 0 :(得分:1)
变得稍微复杂一点。我们需要两个WP_Query对象,一些计算和一个for循环。
为了让PHP了解正确的格式,我们可以使用简单的格式数组。在下面的示例中,我将使用类别来确定两个WP_Query构建。
首先,我们构建基本变量:
<?php
// Build format
// 0 means not featured - 1 means featured.
$format = array(1, 0, 1, 1, 0, 0);
// Retrieve normal posts EXCLUDING category with ID 11 (Featured)
$normal_posts = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 999,
'cat' => '-11'
));
// Retrieve featured posts ONLY INCLUDING category with ID 11 (Featured)
$featured_posts = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 999,
'cat' => 11
));
// Calculate total amount of posts
// from our two WP_Query results
$posts_total = $normal_posts->post_count + $featured_posts;
// Setup current post index
$post_index = 0;
接下来,我们开始循环播放&#39;或迭代:
// Iterate every post from two queries
for($i = 0; $i < $posts_total; $i++){
我们在其中计算当前格式索引:
// Calculate which type of post to display based on post index
// We use modulo to get the right $format index
$post_format_index = $post_index % count($format);
$current_format = $format[$post_format_index];
最后,显示正确的帖子类型:
// Display to frontend based on format
switch ($current_format) {
case 1:
// Featured
$featured_posts->the_post();
the_title();
// Your content
print '<br>';
break;
default:
// Not Featured
$normal_posts->the_post();
the_title();
// Your content
print '<br>';
break;
}
$post_index++;
}
?>
那就是它。您可以通过添加另一个WP_Query和格式来更进一步。
$format = array(1, 0, 1, 1, 2, 2);