【问题标题】:Display only parent custom taxonomy names?仅显示父自定义分类名称?
【发布时间】:2016-10-24 00:08:21
【问题描述】:

我想只显示父类别名称而不是子类别名称 没有在任何产品或帖子中使用,我只需要列出父自定义类别。

我尝试了 get_termswp_list_categories 功能,但它的显示 孩子也

这是我的代码。

<?php 
  require_once('connection.php');
  $taxonomy     = 'product_cat';
  $orderby      = 'parent';
  $show_count   = 0;      // 1 for yes, 0 for no
  $pad_counts   = 0;      // 1 for yes, 0 for no
  $hierarchical = 0;      // 1 for yes, 0 for no
  $title        = '';
  $empty        = 0;

  $args = array(
    'taxonomy'     => $taxonomy,
    'orderby'      => $orderby,
    'show_count'   => $show_count,
    'pad_counts'   => $pad_counts,
    'childless'              => false,
    'child_of'               => 0,
    'title_li'     => $title,
    'hide_empty'   => $empty,
    'hierarchical'=>1
    //'hierarchical=0&depth=1'
  );

  $rrr=wp_list_categories( $args );
  print_r($rrr);
?>

它也显示孩子,但我只需要父类别名称。

我使用 Product_cat 是一个 woocommerce 类别,当我使用它时 get_terms 它被赋予 null 数组。

我也是这样使用的,但是 product_cat 不能使用 get_terms

<?php
$parent_cat_arg = array('hide_empty' => false, 'parent' => 0 );
$parent_cat = get_terms('product_cat',$parent_cat_arg);

foreach ($parent_cat as $catVal) {
  /*some code*/
}
?>

请参阅附件图片,我已经解释了我需要什么。

【问题讨论】:

  • 你试过了吗:hierarchical=0&amp;depth=1
  • 是的,我试过了。

标签: php wordpress


【解决方案1】:

试试这套parent0

 $taxonomy     = 'product_cat';
  $orderby      = 'parent';
  $show_count   = 0;      // 1 for yes, 0 for no
  $pad_counts   = 0;      // 1 for yes, 0 for no
  $hierarchical = 0;      // 1 for yes, 0 for no
  $title        = '';
  $empty        = 0;

  $args = array(
    'taxonomy'     => $taxonomy,
    'orderby'      => $orderby,
    'show_count'   => $show_count,
    'pad_counts'   => $pad_counts,
    'childless'              => 0,
    'child_of'               => 0,
    'title_li'     => $title,
    'hide_empty'   => $empty,
    'parent'=>0,

  );

  $rrr=wp_list_categories( $args );
  print_r($rrr);

【讨论】:

  • 它不工作。 product_cat 是一个 woocommerce 类别,它不适用于 wp_list_categories、get_terms、get_categories。
  • 您在使用 woocommerce 类别吗?
  • 是的,在我的本地电脑中使用 woo commerce 类别。
  • 但是这里不工作我使用了和你一样的代码。
  • @sameersheikh 不客气。我很高兴它有帮助
【解决方案2】:

使用此代码显示父类别名称

<?php
        $terms = get_terms('category', array('hide_empty' => false, 'parent' => 0));
        $terms2 = $terms;
        for ($i = 0; $i < count($terms); $i++) {
            $child = get_terms('category', array('hide_empty' => false, 'parent' => $terms[$i]->term_id));
            $terms2[$i]->child = $child;
        }

        for ($s = (count($terms2) - 1); $s >= 0; $s--) {

            if (isset($terms2[$s]->child)) {
                echo "<select name='parent_cat' class='dropdown' id='cat_" . $terms2[$s]->name . "'>";
                echo "<option selected='selected' value=''>" . $terms2[$s]->name . "</option>";
                for ($ch = 0; $ch < count($terms2[$s]->child); $ch++) {
                    echo "<option value='" . $terms2[$s]->child[$ch]->term_id . "'>" . $terms2[$s]->child[$ch]->name . "</option>";
                }
                echo "</select>";
            }
        }
    ?>

您可以更改自定义帖子类型分类名称的术语。

【讨论】:

    【解决方案3】:

    如果 get_terms 因某些奇怪的原因无法正常工作,自定义分类法未显示已注册,请尝试使用 WP_Term_Query

    $term_query = new WP_Term_Query( array( 
        'taxonomy' => 'regions', // <-- Custom Taxonomy name..
        'orderby'                => 'name',
        'order'                  => 'ASC',
        'child_of'               => 0,
        'parent' => 0,
        'fields'                 => 'all',
        'hide_empty'             => false,
        ) );
    
    
    // Show Array info
    echo "<pre>";
    print_r($term_query->terms);
    echo "</pre>";
    
    
    //Render html
    if ( ! empty( $term_query->terms ) ) {
    foreach ( $term_query ->terms as $term ) {
    echo $term->name .", ";
    echo $term->term_id .", ";
    echo $term->slug .", ";
    echo "<br>";
    }
    } else {
    echo '‘No term found.’';
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-21
      • 1970-01-01
      • 1970-01-01
      • 2021-04-03
      • 2013-03-10
      相关资源
      最近更新 更多