WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. Pete
    Member
    Posted 6 months ago #

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

    ?>

  2. bcworkz
    Member
    Posted 6 months ago #

    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.

  3. Pete
    Member
    Posted 6 months ago #

    I got that code from WP documentation... that's as far as my php skills go :P

  4. bcworkz
    Member
    Posted 6 months ago #

    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.

  5. Pete
    Member
    Posted 6 months ago #

    Thanks for that I'll give i a go

  6. Pete
    Member
    Posted 6 months ago #

    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;
    }
    ?>
  7. bcworkz
    Member
    Posted 6 months ago #

    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.

  8. Pete
    Member
    Posted 6 months ago #

    eh?

  9. bcworkz
    Member
    Posted 6 months ago #

    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;

Reply

You must log in to post.

About this Topic