WP自定义选项卡系统 - 多个自定义帖子类型的多个循环

问题描述 投票:0回答:1

我正在构建一个Wordpress网站,我正在尝试在单个页面上创建一个选项卡结构,我可以在其中创建一种子菜单并显示4种不同的自定义后期类型循环,具体取决于菜单项是什么选择。我已经能够使nav-items块与foreach循环一致,并且内容块正确切换,但是现在,每个内容div显示我拥有的每个自定义帖子类型的所有帖子。

我已经尝试过switch语句和if语句以及is_singular条件标记,但到目前为止没有任何工作正常

我在代码中添加了注释,以便为自己描绘每个部分正在做什么,认为它们可能会帮助您进入我的大脑空间。

        <ul class="tab" role="tablist">
        <?php 

      //All public posts that are not built into Wordpress

        $argsnav = array(
         'public'   => true,
         '_builtin' => false,
        );

      $output = 'objects'; // names or objects, note names is the default
      $operator = 'and'; // 'and' or 'or'

      //Get the Post Types of each post as an object 
      $post_types = get_post_types( $argsnav, $output, $operator ); 

      //Remove Product from Array
      unset( $post_types ['product'] );

      //Iterate through each post type
      foreach ($post_types as $post_type) {

      //And print out a list item with the corresponding ID and text
      echo '<li> 
                <a href="#' . $post_type->name . '" role="tab" data-toggle="tab"> 
                '. $post_type->label .'
                </a> 
            </li>'; 
      } 
      echo '</ul>';
      ?>

     //New section for post content/ This is the buggy portion
     <div class="tab-content">
     <?php

      //Iterate through each post type
        foreach($post_types as &$post_type) { ?>

      <!--Create div with corresponding id-->
        <div class="tab-pane" id="<?php echo "$post_type->name" ?>">

      <!--Create new query for all posts of each type-->       
        <?php $loop = new WP_Query(
              array(
                  'post_type' => array('standup', 'novels', 'short_stories', 'music'),
              )); 

      //Loop through and display each post's title
      if ( $loop->have_posts()) :
        while ( $loop->have_posts() ) :
        $loop->the_post();  ?>

            <h2><?php the_title(); ?></h2>

      <!--Stop the loop-->
      <?php endwhile;  ?>
      <?php endif; ?>
      </div>
      <?php } wp_reset_postdata();  ?>


      </div>

我想我知道需要做些什么,但是我怎么会陷入困境。很感谢任何形式的帮助!

php wordpress loops custom-post-type
1个回答
0
投票

我觉得,你不需要在wp_query中设置所有post_type,请尝试这个

   <!--Create new query for all posts of each type-->       
        <?php $loop = new WP_Query(
              array(
                  'post_type' => $post_type->name,
              )); 

      //Loop through and display each post's title
© www.soinside.com 2019 - 2024. All rights reserved.