分类存档,按其他分类分类,不隐藏空分类

时间:2018-03-26 作者:thetwopct

Info about my setup:

<我有一个名为“治疗”的自定义贴子类型,每个治疗最多可分为3个分类类型-治疗类别-“美容沙龙”、“皮肤病”和“spadays”,它们下面有许多不同的类别
  • 治疗也可以附加到品牌,也可以附加到分类-“品牌”
    • What I am trying to achieve:

      在治疗类别存档页面上显示治疗,但按品牌分类法进行划分,还包括类别内未附加到品牌分类法的治疗。因此,归档页面的外观如下所示:

      Treatment Category Title

      Brand 1

      <治疗A治疗B治疗C治疗

      Brand 2

      <治疗D(无品牌)

      我对治疗J进行了分类。php模板在每个分类页面上都会处理这个问题,但是。。。

      My issues are:

      1) 我无法找到阻止显示空治疗类别标题的方法。我想我需要在某处插入foreach或IF语句,或者可能更改$args数组,以便只返回中包含处理的类别。我尝试过包含hide\\u空语句,但这似乎也不起作用。

      2) 当我按品牌分类法设置查询时,我无法显示未附加到任何品牌的治疗方法。也许我需要两个循环?

      这是我的密码,有什么帮助吗?

      <?php
      
      // grab current page info
      $queried_object = get_queried_object();
      $taxonomy = $queried_object->taxonomy;
      $term_id = $queried_object->term_id;
      
      // fetch the terms for brands tax
      $terms = get_terms( \'brands\', array(
          \'orderby\'    => \'title\',
              \'order\'   => \'ASC\',
              \'hide_empty\' => true,
      ) );
      
      the_archive_title( \'<h1 class="page-title">\', \'</h1>\' );
      
      // now run a query for each brand
      foreach( $terms as $term ) {
      
          $args = array(
              \'post_type\' => \'treatments\',
              \'nopaging\' => true,
              \'orderby\' => \'title\',
              \'order\'   => \'ASC\',
              \'hide_empty\'   => true,
              \'tax_query\' => array(
                  \'relation\' => \'AND\',
                  array(
                      \'taxonomy\' => \'brands\',
                      \'terms\' => $term->slug,
                      \'field\' => \'slug\',
                      \'operator\' => \'IN\'
                  ),
                  array(
                      \'relation\' => \'OR\',
                      array(
                          \'taxonomy\' => \'beautysalon\',
                          \'terms\' => $queried_object->slug,
                          \'field\' => \'slug\',
                          \'operator\' => \'IN\'
                      ),
                      array(
                          \'taxonomy\' => \'skinclinic\',
                          \'terms\' => $queried_object->slug,
                          \'field\' => \'slug\',
                          \'operator\' => \'IN\'
                      ),
                      array(
                          \'taxonomy\' => \'spadays\',
                          \'terms\' => $queried_object->slug,
                          \'field\' => \'slug\',
                          \'operator\' => \'IN\'
                      ),
                  )
              )
          );
          $query = new WP_Query( $args );
      
      // output the term name in a heading tag
      echo\'<h2>\' . $term->name . \'</h2>\';
      // output the post titles in a list
      echo \'<ul>\';
      while ( $query->have_posts() ) : $query->the_post(); ?>
      
              <li id="post-<?php the_ID(); ?>">
                  <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
              </li>
      
              <?php endwhile;
      
          echo \'</ul>\';
          wp_reset_postdata();
      
      } ?>
      

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

    1) 解决这个问题非常简单-我将类别名称请求包装在if have_posts 这样就消除了空值

    2) 我必须使用\'operator\' => \'NOT IN\' 列出不在分类中的处理方法。

    我确信有一种更精简、更好的方法来做以下事情,但现在这正是我想要做的。

    最终代码

    <?php
    
                // grab current page info
                $queried_object = get_queried_object();
                $taxonomy = $queried_object->taxonomy;
                $term_id = $queried_object->term_id;
    
                // fetch the terms for brands tax
                $terms = get_terms( \'brands\', array(
                    \'orderby\'    => \'title\',
                    \'order\'   => \'ASC\',
                    \'hide_empty\' => true,
                    \'posts_per_page\' => -1,
                ) );
    
                the_archive_title( \'<h1 class="page-title">\', \'</h1>\' );
    
                // now run a query for each brand
                foreach( $terms as $term ) {
    
                    $args = array(
                        \'post_type\' => \'treatments\',
                        \'nopaging\' => true,
                        \'orderby\' => \'title\',
                        \'order\'   => \'ASC\',
                        \'hide_empty\'   => true,
                        \'posts_per_page\' => -1,
                        \'tax_query\' => array(
                            array(
                                \'taxonomy\' => \'brands\',
                                \'terms\' => $term->slug,
                                \'field\' => \'slug\',
                                \'operator\' => \'IN\',
                                \'hide_empty\'   => true,
                            ),
                            array(
                                \'relation\' => \'OR\',
                                array(
                                    \'taxonomy\' => \'beautysalon\',
                                    \'terms\' => $queried_object->slug,
                                    \'field\' => \'slug\',
                                    \'operator\' => \'IN\'
                                ),
                                array(
                                    \'taxonomy\' => \'skinclinic\',
                                    \'terms\' => $queried_object->slug,
                                    \'field\' => \'slug\',
                                    \'operator\' => \'IN\'
                                ),
                                array(
                                    \'taxonomy\' => \'spadays\',
                                    \'terms\' => $queried_object->slug,
                                    \'field\' => \'slug\',
                                    \'operator\' => \'IN\'
                                ),
                            )
                        )
                    );
                    $query = new WP_Query( $args );
    
    // only show a header if there is posts present 
    if( $query->have_posts() ) :
    echo\'<h2>\' . $term->name . \'</h2>\';
    echo \'<ul>\';
    while ( $query->have_posts() ) :
    $query->the_post(); ?>
    
                    <li id="post-<?php the_ID(); ?>">
                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                    </li>
    
                <?php endwhile;
    
    echo \'</ul>\';
    endif;
    wp_reset_postdata();
    }
            // second query - everything not in a brand
            $args = array(
                \'post_type\' => \'treatments\',
                \'nopaging\' => true,
                \'orderby\' => \'title\',
                \'order\'   => \'ASC\',
                \'tax_query\' => array(
                    array(
                        \'taxonomy\' => \'brands\',
                        \'terms\' => $term->slug,
                        \'field\' => \'term_id\',
                        \'operator\' => \'NOT IN\',
                    ),
                    array(
                        \'relation\' => \'OR\',
                        array(
                            \'taxonomy\' => \'beautysalon\',
                            \'terms\' => $queried_object->slug,
                            \'field\' => \'slug\',
                            \'operator\' => \'IN\'
                        ),
                        array(
                            \'taxonomy\' => \'skinclinic\',
                            \'terms\' => $queried_object->slug,
                            \'field\' => \'slug\',
                            \'operator\' => \'IN\'
                        ),
                        array(
                            \'taxonomy\' => \'spadays\',
                            \'terms\' => $queried_object->slug,
                            \'field\' => \'slug\',
                            \'operator\' => \'IN\'
                        ),
                    )
                ));
                $the_query = new WP_Query( $args );
    
                if ( $the_query->have_posts() ) :
                    echo \'<h2>Other \' . $queried_object->name . \' Treatments</h2>\';
                    while ( $the_query->have_posts() ) : $the_query->the_post();
                    ?>
    
                    <li id="post-<?php the_ID(); ?>">
                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                    </li>
    
                <?php endwhile;
            endif;
            echo \'</ul>\';
            wp_reset_postdata();
            ?>
    

    结束

    相关推荐

    Posts list in custom taxonomy

    我有这样的事情:$terms = get_the_terms( get_the_ID(), \'kosmetyki_dystrybutor\'); $terms_ids = []; foreach ( $terms as $term ) { $terms_ids[] = $term->term_id; } $args = array( \'post_type\' => \'kosmetyki\',

    分类存档,按其他分类分类,不隐藏空分类 - 小码农CODE - 行之有效找到问题解决它

    分类存档,按其他分类分类,不隐藏空分类

    时间:2018-03-26 作者:thetwopct

    Info about my setup:

    <我有一个名为“治疗”的自定义贴子类型,每个治疗最多可分为3个分类类型-治疗类别-“美容沙龙”、“皮肤病”和“spadays”,它们下面有许多不同的类别
  • 治疗也可以附加到品牌,也可以附加到分类-“品牌”
    • What I am trying to achieve:

      在治疗类别存档页面上显示治疗,但按品牌分类法进行划分,还包括类别内未附加到品牌分类法的治疗。因此,归档页面的外观如下所示:

      Treatment Category Title

      Brand 1

      <治疗A治疗B治疗C治疗

      Brand 2

      <治疗D(无品牌)

      我对治疗J进行了分类。php模板在每个分类页面上都会处理这个问题,但是。。。

      My issues are:

      1) 我无法找到阻止显示空治疗类别标题的方法。我想我需要在某处插入foreach或IF语句,或者可能更改$args数组,以便只返回中包含处理的类别。我尝试过包含hide\\u空语句,但这似乎也不起作用。

      2) 当我按品牌分类法设置查询时,我无法显示未附加到任何品牌的治疗方法。也许我需要两个循环?

      这是我的密码,有什么帮助吗?

      <?php
      
      // grab current page info
      $queried_object = get_queried_object();
      $taxonomy = $queried_object->taxonomy;
      $term_id = $queried_object->term_id;
      
      // fetch the terms for brands tax
      $terms = get_terms( \'brands\', array(
          \'orderby\'    => \'title\',
              \'order\'   => \'ASC\',
              \'hide_empty\' => true,
      ) );
      
      the_archive_title( \'<h1 class="page-title">\', \'</h1>\' );
      
      // now run a query for each brand
      foreach( $terms as $term ) {
      
          $args = array(
              \'post_type\' => \'treatments\',
              \'nopaging\' => true,
              \'orderby\' => \'title\',
              \'order\'   => \'ASC\',
              \'hide_empty\'   => true,
              \'tax_query\' => array(
                  \'relation\' => \'AND\',
                  array(
                      \'taxonomy\' => \'brands\',
                      \'terms\' => $term->slug,
                      \'field\' => \'slug\',
                      \'operator\' => \'IN\'
                  ),
                  array(
                      \'relation\' => \'OR\',
                      array(
                          \'taxonomy\' => \'beautysalon\',
                          \'terms\' => $queried_object->slug,
                          \'field\' => \'slug\',
                          \'operator\' => \'IN\'
                      ),
                      array(
                          \'taxonomy\' => \'skinclinic\',
                          \'terms\' => $queried_object->slug,
                          \'field\' => \'slug\',
                          \'operator\' => \'IN\'
                      ),
                      array(
                          \'taxonomy\' => \'spadays\',
                          \'terms\' => $queried_object->slug,
                          \'field\' => \'slug\',
                          \'operator\' => \'IN\'
                      ),
                  )
              )
          );
          $query = new WP_Query( $args );
      
      // output the term name in a heading tag
      echo\'<h2>\' . $term->name . \'</h2>\';
      // output the post titles in a list
      echo \'<ul>\';
      while ( $query->have_posts() ) : $query->the_post(); ?>
      
              <li id="post-<?php the_ID(); ?>">
                  <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
              </li>
      
              <?php endwhile;
      
          echo \'</ul>\';
          wp_reset_postdata();
      
      } ?>
      

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

    1) 解决这个问题非常简单-我将类别名称请求包装在if have_posts 这样就消除了空值

    2) 我必须使用\'operator\' => \'NOT IN\' 列出不在分类中的处理方法。

    我确信有一种更精简、更好的方法来做以下事情,但现在这正是我想要做的。

    最终代码

    <?php
    
                // grab current page info
                $queried_object = get_queried_object();
                $taxonomy = $queried_object->taxonomy;
                $term_id = $queried_object->term_id;
    
                // fetch the terms for brands tax
                $terms = get_terms( \'brands\', array(
                    \'orderby\'    => \'title\',
                    \'order\'   => \'ASC\',
                    \'hide_empty\' => true,
                    \'posts_per_page\' => -1,
                ) );
    
                the_archive_title( \'<h1 class="page-title">\', \'</h1>\' );
    
                // now run a query for each brand
                foreach( $terms as $term ) {
    
                    $args = array(
                        \'post_type\' => \'treatments\',
                        \'nopaging\' => true,
                        \'orderby\' => \'title\',
                        \'order\'   => \'ASC\',
                        \'hide_empty\'   => true,
                        \'posts_per_page\' => -1,
                        \'tax_query\' => array(
                            array(
                                \'taxonomy\' => \'brands\',
                                \'terms\' => $term->slug,
                                \'field\' => \'slug\',
                                \'operator\' => \'IN\',
                                \'hide_empty\'   => true,
                            ),
                            array(
                                \'relation\' => \'OR\',
                                array(
                                    \'taxonomy\' => \'beautysalon\',
                                    \'terms\' => $queried_object->slug,
                                    \'field\' => \'slug\',
                                    \'operator\' => \'IN\'
                                ),
                                array(
                                    \'taxonomy\' => \'skinclinic\',
                                    \'terms\' => $queried_object->slug,
                                    \'field\' => \'slug\',
                                    \'operator\' => \'IN\'
                                ),
                                array(
                                    \'taxonomy\' => \'spadays\',
                                    \'terms\' => $queried_object->slug,
                                    \'field\' => \'slug\',
                                    \'operator\' => \'IN\'
                                ),
                            )
                        )
                    );
                    $query = new WP_Query( $args );
    
    // only show a header if there is posts present 
    if( $query->have_posts() ) :
    echo\'<h2>\' . $term->name . \'</h2>\';
    echo \'<ul>\';
    while ( $query->have_posts() ) :
    $query->the_post(); ?>
    
                    <li id="post-<?php the_ID(); ?>">
                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                    </li>
    
                <?php endwhile;
    
    echo \'</ul>\';
    endif;
    wp_reset_postdata();
    }
            // second query - everything not in a brand
            $args = array(
                \'post_type\' => \'treatments\',
                \'nopaging\' => true,
                \'orderby\' => \'title\',
                \'order\'   => \'ASC\',
                \'tax_query\' => array(
                    array(
                        \'taxonomy\' => \'brands\',
                        \'terms\' => $term->slug,
                        \'field\' => \'term_id\',
                        \'operator\' => \'NOT IN\',
                    ),
                    array(
                        \'relation\' => \'OR\',
                        array(
                            \'taxonomy\' => \'beautysalon\',
                            \'terms\' => $queried_object->slug,
                            \'field\' => \'slug\',
                            \'operator\' => \'IN\'
                        ),
                        array(
                            \'taxonomy\' => \'skinclinic\',
                            \'terms\' => $queried_object->slug,
                            \'field\' => \'slug\',
                            \'operator\' => \'IN\'
                        ),
                        array(
                            \'taxonomy\' => \'spadays\',
                            \'terms\' => $queried_object->slug,
                            \'field\' => \'slug\',
                            \'operator\' => \'IN\'
                        ),
                    )
                ));
                $the_query = new WP_Query( $args );
    
                if ( $the_query->have_posts() ) :
                    echo \'<h2>Other \' . $queried_object->name . \' Treatments</h2>\';
                    while ( $the_query->have_posts() ) : $the_query->the_post();
                    ?>
    
                    <li id="post-<?php the_ID(); ?>">
                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                    </li>
    
                <?php endwhile;
            endif;
            echo \'</ul>\';
            wp_reset_postdata();
            ?>
    

    相关推荐

    如何控制根据Taxonomy术语显示什么模板?

    我正在建立一个商业目录,并将给出一些背景,然后在最后有2个问题。The development URL is: http://svcta.lainternet.biz/The website I am rebuilding is: https://www.visitsimivalley.com/当前网站要求每个分类法类型具有唯一的业务概要文件。例如,如果您是一家酒店,并且您也有会议室和婚礼场地,那么您最终会得到3个列表,一个用于酒店,一个用于会议,一个用于婚礼。我希望有一个主配置文件,其中包含我们将显示的