循环访问CPT的类别

时间:2020-11-11 作者:Natasha Mcd

我正在尝试使用Foundations框架创建选项卡,从名为testimonial 并通过其指定的自定义类别(称为filed-under. 我在循环中做错了什么?

What I Have Working

<创建新类别时,会出现类别选项卡

What is Not Working

<这些帖子没有出现在选项卡式内容区域中,您如何让循环过滤“所有”类别Link to Demohttp://staging-newmummycompanyca.temp312.kinsta.cloud/testimonials/

Code

        <?php
        // TABBED HEADERS
        echo \'<ul class="tabs" data-tabs id="example-tabs">\';
        echo \'<li class="tabs-title is-active link-no-style">\';
        echo \'<a href="#all" aria-selected="true">All</a>\';
        echo \'</li>\';
        $args = array(
          \'hide_empty\' => 1,
          \'orderby\'   => \'name\',
          \'order\'     => \'ASC\',
          \'post_type\' => \'testimonial\',
          \'taxonomy\'  => \'filed-under\',
        );
        $categories = get_categories($args);
        foreach ($categories as $category) {
          echo
            \'<li class="tabs-title link-no-style">
                <a href="#\' . $category->slug . \'" aria-selected="true" role="tab" data-toggle="tab">    
                  \' . $category->name . \'
                </a>
              </li>\';
        }
        echo \'</ul>\';

        // TABBED CONTENT
        echo \'<div class="tabs-content" data-tabs-content="example-tabs">\';
        echo \'<div class="tabs-panel is-active" id="\' . $category->slug . \'">\';
        echo \'<ul class="accordion" data-accordion data-allow-all-closed="true">\';
        foreach ($categories as $category) {
          $the_query = new WP_Query(array(
            \'post_type\'     => \'testimonial\',
            \'post_status\'   => \'publish\',
            \'category_name\' => $category->slug,
          ));
          while ($the_query->have_posts()) : $the_query->the_post();
            echo \'<li class="accordion-item" data-accordion-item>\';
            echo \'<a href="#" class="accordion-title"><p>\';
            the_title();
            echo \'</p></a>\';
            echo \'<div class="accordion-content" data-tab-content>\';
            echo the_field(\'testimonial\');
            echo \'</div>\';
            echo \'</li>\';
          endwhile;
          wp_reset_postdata();
        }
        echo \'</ul>\';
        echo \'</div>\';
        echo \'</div>\';
        ?>
<小时/>

CODE V2

<?php 
        echo \'<ul class="tabs" data-tabs id="example-tabs">\';
        echo \'<li class="tabs-title is-active link-no-style">\';
        echo \'<a href="#" aria-selected="true">All</a>\';
        echo \'</li>\';  

        $_terms = get_terms(array(\'filed-under\'));
        foreach ($_terms as $term) {

        // TABBED HEADERS 
        echo \'<li class="tabs-title link-no-style">\';
        echo \'<a href="#\' . $term->slug . \'"aria-selected="true" role="tab" data-toggle="tab">\';
        echo $term->name;
        echo \'</a>\';
        echo \'</li>\';
        }  // CLOSE OFF FIRST LOOP 
        echo \'</ul>\';

        foreach ($_terms as $term) :
        $term_slug = $term->slug;
        
        // QUERY
        $_posts = new WP_Query( array(
          \'post_type\'         => \'testimonial\',
          \'posts_per_page\'    => -1, 
          \'tax_query\' => array(
            array(
              \'taxonomy\' => \'filed-under\',
              \'field\'    => \'slug\',
              \'terms\'    => $term_slug,
            ),
          ),
        )); 
        
        // TABBED CONTENT
        echo \'<div class="tabs-content" data-tabs-content="example-tabs">\';
        echo \'<div class="tabs-panel" id="\' . $term_slug . \'">\';
        echo \'<ul class="accordion" data-accordion data-allow-all-closed="true">\';
        if( $_posts->have_posts() ) :  while ( $_posts->have_posts() ) : $_posts->the_post(); 
        echo \'<li class="accordion-item" data-accordion-item>\';
        echo \'<a href="#" class="accordion-title">\';
        echo \'<p>\';
        the_title(); 
        echo \'</p>\';
        echo \'</a>\';
        echo \'<div class="accordion-content" data-tab-content>\';
        echo the_field(\'testimonial\');
        echo \'</div>\';
        echo \'</li>\'; 
        endwhile; endif; wp_reset_postdata();
        echo \'</ul>\';
        echo \'</div>\';
        echo \'</div>\';
        endforeach; 
        ?>

1 个回复
最合适的回答,由SO网友:Tom J Nowell 整理而成

\' publish\',
这是您的问题,一个额外的空格字符。

请注意,将来您可以通过使用查询监视器和检查是否找到帖子来捕获这一点。因为post循环缺少if ( $query->have_posts() ) { ... } else { echo "none found"; } 类型检查,您无法知道在哪里查找问题。

此外,您还在filed-under 分类法:

\'taxonomy\'  => \'filed-under\',
但你要把这些条款传给category_name 参数,该参数不相关。因此,现在这些术语的使用就像它们是类别分类法中的术语一样,但它们不是。

相关推荐

do_action not working in loop

我一直在使用do\\u action在循环中生成多个动作挂钩。对我来说,do\\u操作不起作用似乎很不幸。function ltc_generate_input_fields($fields = array()) { echo \"Hello World\"; if (empty($fields) || !is_array($fields)) return; foreach($fields as $field) { &#