3
votes

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

3
Did you try : hierarchical=0&depth=1 ? – Milap
yes i have tried it . – Samir Sheikh

3 Answers

4
votes

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);
1
votes

use this code to display parent category name

<?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>";
            }
        }
    ?>

terms where you can change your custom post type taxonomy name.

0
votes

If get_terms doesnt works for some odd reason with custom taxonomy not showing registered try using 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.’';
}