sameer sheikh sameer sheikh - 5 months ago 30
PHP Question

Display only parent custom taxonomy names?

I would like to display only parent category name not child and also
that not used in any product or post i just need only listing of parent custom category.

i have try get_terms, wp_list_categories function but its showing
child also

This is my code.

<?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);
?>


It's showing child also but i need only parent category name.


i used Product_cat is a woocommerce category and when i used it with
get_terms it's given null array.

i used like this way also but product_cat is not working with 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*/
}
?>


See Attached image also i have explain what i need.

enter image description here

Answer

Try this set parent is 0

 $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);