我想创建一个Wordpress网站,我想展示" tiles"来自首页网站的内容。这些图块是来自网站的自定义帖子类型,例如"我们的服务","顾问","博客帖子"等等。
我知道如何在Wordpress中显示一个自定义帖子类型,但问题是我需要在同一个循环中拉出多个帖子类型,因为我希望它们以矩阵形式显示。另一个问题是我需要以随机顺序对所有项目进行随机播放,以便例如并非所有博客都只显示在一个地方,而是所有对象随机显示在不同的项目之后。
第三个问题是我需要显示某个帖子类型的所有项目,而只显示另一个项目的最新项目。例如,我是否需要显示所有"我们的服务"瓷砖,但只有几个博客"瓦片。
这可能吗,或者你不能用Wordpress以这种方式提取记录吗?
感谢您的帮助!
答案 0 :(得分:3)
我建议您阅读自定义wordpress查询https://codex.wordpress.org/Class_Reference/WP_Query
对于第一个问题,您只需要指定
'post_type' => array( 'tiles', 'consultants', 'post' )
第二个问题
'orderby' => 'rand'
所以你会有像
这样的东西$args = array(
'post_type' => array( 'tiles', 'consultants', 'post' ),
'orderby' => 'rand'
);
$query = new WP_Query( $args );
对于第三个问题 - 我不确定是否可以通过一个查询实现。
答案 1 :(得分:0)
你可以自定义这样的东西,
$posttypes = array('post_typ1','post_typ2','post_typ3');
$randompost_typs = shuffle($posttypes);
$counter = count($posttypes);
for($i=0; $i<$counter;$i++) {
// suppose you want to show all posts from post_type1 then
if($randompost_typs[$i]=='post_typ1') {
$posts_per_page = -1;
} elseif($randompost_typs[$i]=='post_typ2') { // will work for 2nd post type
$post_per_page = 5; // show 5 posts from this post type
} else {
$post_per_page = 3; // show 3 posts from last post type
}
// here you will use the WP_Query class from wordpress
$args = array(
'post_type' => $posttypes[$i],
'orderby' => 'rand',
'posts_per_page' => $post_per_page
);
$query = new WP_Query( $args );
if($query->have_posts()) : while($query->have_posts()): $query->the_post();
// all the remaining wp loop content for example
the_title();
the_excerpt();
endwhile;
else:
echo 'no posts';
endif;
}
希望这会有所帮助,如果有任何问题,请告诉我。