0

I've created a custom post with categories like books, foods, and make some sub-categories like in foods cat, pizza, fastfood. But when I display them in my side bar, it's showing same as sub category. Just like:

.books

.foods

.pizza

.fastfood

I want them to display like this:

.books

.foods

-pizza

-fastfood

Here is my code of functions.php

add_action('init', 'products_register');

function products_register() {
    $labels = array(
        'name' => _x('products', 'post type general name'),
        'singular_name' => _x('Products Item', 'post type singular name'),
        'add_new' => _x('Add New', 'products item'),
        'add_new_item' => __('Add New products Item'),
        'edit_item' => __('Edit products Item'),
        'new_item' => __('New products Item'),
        'view_item' => __('View products Item'),
        'search_items' => __('Search products'),
        'not_found' =>  __('Nothing found'),
        'not_found_in_trash' => __('Nothing found in Trash'),
        'parent_item_colon' => ''
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        //'menu_icon' => get_stylesheet_directory_uri() . '/article16.png',
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array('title','editor','thumbnail')
    ); 

    register_post_type( 'products' , $args );

    register_taxonomy("productcategory", array("products"), array("hierarchical" => true, "label" => "Product Category", "singular_label" => "Product Category", "rewrite" => true));

}

Here is my code to display the category on my sidebar:

<?php

$taxonomy = 'productcategory';
$terms = get_terms($taxonomy); // Get all terms of a taxonomy

if ( $terms && !is_wp_error( $terms ) ) :
?>
    <ul>
        <?php foreach ( $terms as $term ) { ?>
            <li><a href="<?php echo get_term_link($term->slug, $taxonomy); ?>"><?php echo $term->name; ?></a></li>
        <?php } ?>
    </ul>
<?php endif;?>
| improve this question | | | | |
  • Revisit the register_taxonomy codex article. You have some issues in your registration - mainly the labels should be inside a labels array. – Eric Holmes May 26 '14 at 13:46
  • can you show me code, sorry i am new in PHP . and also edit my Question – fzishk May 26 '14 at 13:51
1

Use wp_list_categories. It automatically builds a hierarchical list, and links to them. This should do everything you want!

| improve this answer | | | | |
  • 1
    Eric Holmes, this is not post category, this is custom post category. – fzishk May 26 '14 at 15:46
  • Look at the documentation. You can specify taxonomy. – Eric Holmes May 26 '14 at 15:54
1

Try this,

$show_count = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$taxonomy = 'productcategory';
$title = '';
$args = array(
  'orderby' => $orderby,
  'show_count' => $show_count,
  'hierarchical' => $hierarchical,
  'taxonomy' => $taxonomy,
  'title_li' => $title
);
<ul>
<?php
    wp_list_categories($args);
?>
</ul>

I've tried this and it is working fine. I hope this will help.

| improve this answer | | | | |
1

You can use the WordPress function wp_list_categories($args) to create what you are looking for.

When you register your taxonomy, you also need to set hierarchical to true to be able to nest your Custom Post Type categories.

After, you can just create a function like this one (With basics $args) :

<?php 
$args = array(
    'hierarchical' => true,
    'taxonomy' => 'productcategory',
    'hide_empty' => false
);

wp_list_categories($args);

?>

Not that we are using the 'taxonomy' => 'productcategory', parameter to tell the function to look on the taxonomy itself.

Your HTML output will looks like this.

<ul>    
    <li class="cat-item cat-item-18"><a href="http://wordpress.local/productcategory/books/" >Books</a></li>
    <li class="cat-item cat-item-19"><a href="http://wordpress.local/productcategory/foods/" >Foods</a>
        <ul class='children'>
            <li class="cat-item cat-item-21"><a href="http://wordpress.local/productcategory/fastfood/" >Fastfood</a></li>
            <li class="cat-item cat-item-20"><a href="http://wordpress.local/productcategory/pizza/" >Pizza</a></li>
        </ul>
    </li>
</ul>

The way that you initaly developped this is good to get a list. But it won't display it as hierarchical elements.

After, you will just need to play arround with CSS to add padding on .children class.

HTML Output of the code

Hope it help.

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.