Wordpress循环显示多种帖子类型


Wordpress show multiple post types in loop

我正在创建一个Wordpress网站,如果我想在首页上显示带有该网站内容的"互动程序"。这些贴砖是网站上的自定义帖子类型,如"我们的服务"、"顾问"、"博客文章"等等

我知道如何在Wordpress中显示一个自定义的帖子类型,但问题是我需要在同一个循环中提取多个帖子类型,因为我希望它们显示在矩阵中。另一个问题是,我需要按随机顺序打乱所有项目,这样,例如,不是所有博客都只显示在一个地方,而是所有对象都随机显示在不同项目之后。

第三个问题是,我需要显示某个帖子类型的所有项目,而只显示另一个帖子的最新项目。例如,我需要显示所有的"我们的服务"磁贴,但只显示几个"博客"磁贴。

这可能做到吗?或者你可以不使用Wordpress以这种方式提取记录吗?

谢谢你的帮助!

我建议阅读自定义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 );

对于第三个问题,我不确定是否可以通过一个查询实现。

您可以自定义这样的东西,

  $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;
       }

希望这会有所帮助,如果有任何问题,请告诉我。