WordPress.org

Support

Support » Plugins and Hacks » Hacks » Howe to Display defined Category (and all child cats) Assigned to a Post

Howe to Display defined Category (and all child cats) Assigned to a Post

  • I’m using this piece of code but i’m trying to hack it so that it only displays a specified category (manually)… and all it’s child categories(automatically).

    <?php
    $taxonomy = 'category';
    
    // get the term IDs assigned to post.
    $post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
    // separator between links
    $separator = ', ';
    
    if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {
    
    	$term_ids = implode( ',' , $post_terms );
    	$terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids );
    	$terms = rtrim( trim( str_replace( '<br />',  $separator, $terms ) ), $separator );
    
    	// display post categories
    	echo  $terms;
    }

    ?>

Viewing 8 replies - 1 through 8 (of 8 total)
  • You use the ‘child_of’ argument to wp_list_categories() to list the children, but you can only specify a single parent ID. The script currently uses potentially multiple ID’s to specify what’s listed, but it sounds like you’re altering that as well, so it may work out.

    It appears you’ll need to call wp_list_categories() twice. Once for the manual category, then again for the children. I don’t think there’ll be much left of that script once the needed changes are made. You may be better off starting from scratch IMO.

    I got that code from WP documentation… that’s as far as my php skills go 😛

    I’m not sure how you are manually specifying a category, I’ll just hardcode it here. This is all you really need:

    $cat_id = 69;
    wp_list_categories('include='.$cat_id );
    wp_list_categories('child_of='.$cat_id );

    Anything else is for altering how the list appears.

    Thanks for that I’ll give i a go

    I tried this… (But it it displayed “no categories”)

    <?php
    $taxonomy = 'category';
    
    // get the term IDs assigned to post.
    $post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
    // separator between links
    $separator = ', ';
    
    if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {
    	$cat_id = 2;
    	$term_ids = implode( ',' , $post_terms );
    	$terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $cat_id . 'child_of='.$cat_id);
    	$terms = rtrim( trim( str_replace( '<br />',  $separator, $terms ) ), $separator );
    
    	// display post categories
    	echo  $terms;
    }
    ?>

    You can’t have ‘child_of’ and ‘include’ arguments with the same value together in the same call, they are mutually exclusive. Make two separate calls, doing the rtrim and echo to the results of each.

    eh?

    Separate calls with separate arguments like this:

    $terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $cat_id );
    $terms = rtrim( trim( str_replace( '<br />',  $separator, $terms ) ), $separator );
    echo  $terms;
    $terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . 'child_of=' . $cat_id );
    $terms = rtrim( trim( str_replace( '<br />',  $separator, $terms ) ), $separator );
    echo  $terms;

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Howe to Display defined Category (and all child cats) Assigned to a Post’ is closed to new replies.