我有点难过这个。我正在使用wordpress,我想使用某种比率查询多个自定义帖子类型并将其显示在页面上。
假设我有以下帖子类型:
推文,项目,视频&交
目前我的查询结果充满了推文,所有其他帖子类型都被推到了页面上。
有没有办法制定一个查询,以便(比方说)25%的结果被推文占用,剩下的75%被其他帖子类型占用?基本上我想减少查询结果中出现的推文数量。
我希望这是有道理的。
非常感谢
答案 0 :(得分:0)
我可能会使用2个单独的自定义查询(使用WP_Query)手动处理此问题。我假设你想要启用分页(因为它是你唯一的博客卷,最终会变得非常大)。一般来说,策略将是:
1)构建两个不同的查询(1个用于您的项目,视频和帖子......还有1个用于您的推文)。
2)使用查询参数的posts_per_page
属性设置您想要混合的比率。
3)循环每个查询,但不要输出他们的标题或内容,而是将每个查询或内容存储在自己的数组中。
4)创建一个 new 数组,以包含你的" shuffled"志串连。
5)遍历此 new 数组以输出内容。
$posts_array = array(); // this will temporarily hold your projects/videos/posts
$tweets_array = array(); // this will temporarily hold your tweets
// WP_Query arguments for projects, videos, and posts
$post_args = array (
'post_type' => array('project', 'video', 'post'),
'post_status' => 'publish',
'pagination' => true,
'posts_per_page' => '10',
);
// The Query
$query_posts = new WP_Query( $post_args );
// The Loop will be used to grab the posts and store them in $posts_array
if ( $query_posts->have_posts() ) {
while ( $query_posts->have_posts() ) {
$query_posts->the_post();
// here you can use any number of functions inside the loop to get content or metadata about this post
$posts_array[] = array(
'title' => get_the_title(),
'permalink' => get_permalink(),
'excerpt' => get_the_excerpt(),
'content' => get_the_content(),
'thumbnail' => get_the_post_thumbnail()
);
}
} else {
// no posts found, maybe handle this differently if you wish
}
// Restore original Post Data
wp_reset_postdata();
// WP_Query arguments just for tweets
$tweet_args = array (
'post_type' => 'tweet'
'post_status' => 'publish',
'pagination' => true,
'posts_per_page' => '2',
);
// The Query
$query_tweets = new WP_Query( $tweet_args );
// The Loop will be used for your tweets
if ( $query_tweets->have_posts() ) {
while ( $query_tweets->have_posts() ) {
$query_tweets->the_post();
$tweets_array[] = array(
'title' => get_the_title(),
'permalink' => get_permalink(),
'excerpt' => get_the_excerpt(),
'content' => get_the_content(),
'thumbnail' => get_the_post_thumbnail()
);
}
} else {
// no posts found, maybe handle this differently if you wish
}
// Restore original Post Data
wp_reset_postdata();
$final_array = array(); //this is the array that'll hold the shuffled post types
// this loop is currently built to assume the "posts_per_page" ratio from above
// edit these to your liking if you want to increase the number of tweets per 10 other posts
for ($i = 0; $i < 10; $i++) {
$final_array[] = $posts_array[$i];
// after the 5th normal post, inject a tweet (we only have 2 tweets, so inject the first one)
if ($i == 4) {
$final_array[] = $tweets_array[0];
}
}
// now that all 10 posts are done, throw in the last tweet next
$final_array[] = $tweets_array[1];
// now output your content in your template
foreach ($final_array as $current_post) {
// this will be your markup for each post so replace this with the HTML for your own template
echo '<div class="title"><a href="' . $current_post['permalink'] . '">' . $current_post['title'] . '</a></div>';
echo '<div class="excerpt">' . $current_post['excerpt'] . '</div>';
// etc etc with any of your remaining array elements
}
我没有测试过这段代码,所以请把它当作伪代码。但总的想法很有希望。
玩得开心!