【问题标题】:WP Custom Tabs System- Multiple Loops for Multiple Custom Post TypesWP 自定义标签系统 - 多个自定义帖子类型的多个循环
【发布时间】:2018-02-05 02:35:17
【问题描述】:

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

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

我在代码中加入了 cmets 来为自己描述每个部分在做什么,我认为它们可能会帮助您进入我的大脑空间。

        <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】:

    我觉得,你不需要在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
    

    【讨论】:

    • 做到了!非常感谢,我现在看到如何通过设置它们,我将所有帖子类型放在那里,而不是让查询单独调用它。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多