How to show parent/child relationship between categories in a list

I am using this code to display a list of categories in my classifieds section:

<ul>
                <?php $taxonomies = get_object_taxonomies( 'classifieds', 'objects' ); ?>
                <?php foreach ( $taxonomies as $taxonomy_name => $taxonomy_object ): ?>
                    <?php $terms = get_terms( $taxonomy_name, array( 'hide_empty' => 0 ) ); ?>
                        <?php foreach ( $terms as $term ): ?>
                            <li><a href="<?php echo get_term_link( $term, $taxonomy_name ); ?>"><?php echo $term->name; ?></a></li>
                        <?php endforeach; ?>
                    <?php endforeach; ?>
                </ul>

Is there a way to make the list show the parent / child relationship between the categories?
So for example:
Cycling
-Cyclocross
-DH
-Mountain
-Road

Thanks!

  • 3SixtyEvolve

    Hi mccawphoto

    Welcome to the WPMU DEV Community and thank you for being a member!

    I had a look at your code and there are a couple of ways to achieve what you would like to do. I have this bit of code that might be helpful:

    <?php if ($this_category->category_parent == 0) { ?>
     <?php wp_list_cats('child_of='.$cat.'&hide_empty=0&sort_column=name'); ?>
     <?php } else {?>
     <?php wp_list_cats('child_of='.$this_category->category_parent.'&hide_empty=0&sort_column=name'); ?>
     <?php } ?>

    And then you can also check out this link: http://codex.wordpress.org/Template_Tags/wp_list_categories

    In the meantime I will flag the Developer to also provide feedback and we should have you sorted in no time.

    Have a good day and be in touch shortly with more feedback.

    GinaB

  • allanmcnabb

    Thanks!

    From the link I suppose this would get me started:

    <?php
    //list terms in a given taxonomy using wp_list_categories (also useful as a widget if using a PHP Code plugin)

    $taxonomy = 'classifieds';
    $orderby = 'name';
    $show_count = 0; // 1 for yes, 0 for no
    $pad_counts = 0; // 1 for yes, 0 for no
    $hierarchical = 1; // 1 for yes, 0 for no
    $title = '';

    $args = array(
    'taxonomy' => $taxonomy,
    'orderby' => $orderby,
    'show_count' => $show_count,
    'pad_counts' => $pad_counts,
    'hierarchical' => $hierarchical,
    'title_li' => $title
    );
    ?>

      <?php wp_list_categories( $args ); ?>

    I'm using buddy press. Can you give me a suggestion how to deal with putting this in a template? I assume the template would only be used for the page (s) I would want the list to appear.

    Thanks!
    Allan

  • Mason

    Hiya Allen,

    Can you give me a suggestion how to deal with putting this in a template? I assume the template would only be used for the page (s) I would want the list to appear.

    Do you want it to appear on Classifieds pages?

    If so, in the classifieds plugin go to the classifieds/ui-front directory. You'll see file templates for BuddyPress and general WordPress. Make a copy off the ones you want to use in your theme's root directory and add your new query.

    You should be good to go.

  • allanmcnabb

    Thanks Mason!

    I'm new to WordPress and BuddyPress, and I'm trying to wrap my head around your answer, which sounds very good.

    Yes, I would like a list of Classifieds categories to appear on the BuddyPress Classifieds page (http://lol-lutz-wc.ourtownplus.com/classifieds/) - parent and child - not listing empty categories.

    I have 9 php files at /wp-content/plugins/classifieds/ui-front/general
    - author.php
    - custom-fields.php
    - loop-author.php
    - loop-taxonomy.php
    - page-checkout.php
    - page-classifieds.php
    - page-my-classifieds.php
    - single-classifieds.php
    - taxonomy.php

    And I have 5 php files at /wp-content/plugins/classifieds/ui-front/buddypress/members/single/classifieds
    - create-new.php
    - custom-fields.php
    - edit-ad.php
    - my-classifieds.php
    - my-credits.php

    Can you give me a little more info about the process?
    - Which file should I try to use?
    - Do I put the whole file in the root directory of the theme, or add it to a default theme?
    - What would the query be and where should I put it?

    Thanks for your help!
    Allan

  • Mason

    Hiya,

    If all you want is for the listing to show on that one page, then the only file you'll need to copy is page-classifieds.php.

    Just drop it in the root of your theme folder open it up and add your query wherever you want it to display. You will probably have to adjust it a bit to get it to look the way you want.

    In terms of exactly what function to use and exactly where to put it I'm afraid that's outside what we can support here. You'd need to find someone to walk you through that either from our Job Boards:
    https://premium.wpmudev.org/wpmu-jobs/

    or a service like this one:
    http://tweaky.com

    Thanks

  • allanmcnabb

    Solution:

    See
    PHP Code for posts
    http://wordpress.org/extend/plugins/php-code-for-posts/

    The PHP code I put in the plugin is:
    <?php
    //list terms in a given taxonomy using wp_list_categories (also useful as a widget if using a PHP Code plugin)

    $taxonomy = 'classifieds_categories';
    $orderby = 'name';
    $show_count = 1; // 1 for yes, 0 for no
    $pad_counts = 0; // 1 for yes, 0 for no
    $hierarchical = 1; // 1 for yes, 0 for no
    $title = '';

    $args = array(
    'taxonomy' => $taxonomy,
    'orderby' => $orderby,
    'show_count' => $show_count,
    'pad_counts' => $pad_counts,
    'hierarchical' => $hierarchical,
    'title_li' => $title
    );
    ?>

      <?php wp_list_categories( $args ); ?>
  • Arnold

    i indicated the function above.

    http://codex.wordpress.org/Template_Tags/wp_list_categories

    It's a template tag so you can use it in any template you like. Maybe easier would be to make a shortcode. Then you can just put the shortcode in a page [cf_categories] as in the example below. Put that code in your functions.php for your theme.

    add_shortcode('cf_categories', 'classifieds_categories_sc');
    function classifieds_categories_sc( $atts, $content = null ) {
    	extract( shortcode_atts( array(
    	), $atts ) );
    
    	$args = array(
    	'show_option_all'    => '',
    	'orderby'            => 'name',
    	'order'              => 'ASC',
    	'style'              => 'list',
    	'show_count'         => 1,
    	'hide_empty'         => 0,
    	'use_desc_for_title' => 1,
    	'child_of'           => 0,
    	'feed'               => '',
    	'feed_type'          => '',
    	'feed_image'         => '',
    	'exclude'            => '',
    	'exclude_tree'       => '',
    	'include'            => '',
    	'hierarchical'       => true,
    	'title_li'           => __( 'Categories' ),
    	'show_option_none'   => __('No categories'),
    	'number'             => null,
    	'echo'               => 1,
    	'depth'              => 0,
    	'current_category'   => 0,
    	'pad_counts'         => 1,
    	'taxonomy'           => 'classifieds_categories',
    	'walker'             => null
    	);
    	ob_start();
    	wp_list_categories($args);
    	$result = ob_get_contents();
    	ob_end_clean();
    	return $result;
    
    }
  • Mason

    Hiya,

    I think we've shown we're willing to help out as much as possible, but what you're paying for is guaranteed support, scheduled live chats and ongoing updates and new products. What you're not paying for is customization (such as this).

    Support is defined as anytime the product is not behaving the way it's advertised "out of the box". If it's not working for you, we're here to help and will do everything possible to get it working.

    Custom work is when you want to do something that isn't available in the product by default. We can't guarantee you perfect answers in that area. To do so would mean we'd have to be able to handle every feature request and we're simply not staffed to be able to do that.

    We've provided a lot of pointers here to get you going in the right direction OR enough that should allow a developer to handle this for you very quickly.

    Hope that clarifies things a bit.

  • allanmcnabb

    Hi Mason,

    Your company should be so specific when "selling" the service. And besides, the solution was easy (above), which you guys should have been able to relay instantly, and I believe should even be part of your tutorials.

    Again, the plugin is not very helpful without this function, evidenced from all the requests I've seen for information. You guys should have figured this out a long time ago and been proactive with the solution, and even have updated the plugin to do this.

    I hope you understand the frustration of a paying customer, when the simple things are not taken care of, and we receive uninformed answers from people who are supposed to be the "experts" with these products.

    I've had a great experience with many things concerning your company, but I think you fell short on this one. I think you should learn from it, not try to offer excuses, and take the fix given you by a newbie to help your other customers.

    Rather than trying to justify yourself, I think a "thank you" would have been more appropriate.

    Now for a thank you to Arnold. Arnold has been very helpful as we are working to implement a number of plugins from the company, and he has proven himself to be a wonderful asset to WPMU Dev. His fix above looks like it would be better for us than the one I came up with using the plugin PHP Code for posts, but we'll have to try it. Thanks Arnold for your work, and for all you have done to help us. We look forward to working with you in the future!

    Allan

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.