WordPress.org

Forums

Exclude categoies from wp_list_categories? (21 posts)

  1. zebrastribe
    Member
    Posted 11 months ago #

    Hi all

    I am using this from the codex:

    <?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( '',  $separator, $terms ) ), $separator );
    
    	// display post categories
    	echo  $terms;
    }
    ?>

    [Moderator Note: Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum's parser.]

    But I have a hard time adding a exclude to hide some categories from the list.
    I tried this:
    $terms = wp_list_categories( 'title_li=&exclude=1&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids );
    and
    $terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids . '&exclude=1');

    Why is the category not excluded from the list?

  2. esmi
    Forum Moderator
    Posted 11 months ago #

    Are you trying to exclude category 1?

  3. zebrastribe
    Member
    Posted 11 months ago #

    Yes. I also need to specify more categories later. Maybe as a variable $exclude_cat?

  4. esmi
    Forum Moderator
    Posted 11 months ago #

    What happens if you just use:

    $terms = wp_list_categories( 'title_li=&exclude=1&style=none&echo=0);

  5. zebrastribe
    Member
    Posted 11 months ago #

    All the categories from the whole site is shown and the "1" is excluded.

    What I need is:
    Only the associated (to the post) categories with "1" excluded.
    Like this from the codex
    But I need to exclude one or more...

  6. esmi
    Forum Moderator
    Posted 11 months ago #

    Best guess - there's an issue with your creation of either $taxonomy or $term_ids which is causing wp_list_categories() to fail. Have you tried examining the content of these variables?

  7. zebrastribe
    Member
    Posted 11 months ago #

    No. Howto?

    I got this from the codex:
    exclude
    (string) Exclude one or more categories from the results. This parameter takes a comma-separated list of category ids. The parameter include must be empty

    And the example is like this:
    $terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids );

    is the include parameter empty if?
    '&include=' . $term_ids

  8. esmi
    Forum Moderator
    Posted 11 months ago #

    Try:

    <?php
    
    // 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 );
    	echo '<pre>';
    	print_r($term_ids);
    	echo '</pre>';
    
    	$terms = wp_list_categories( 'title_li=&style=none&echo=0&include=' . $term_ids );
    	$terms = rtrim( trim( str_replace( '',  $separator, $terms ) ), $separator );
    
    	// display post categories
    	echo  $terms;
    }
    ?>

    You don't need $taxonomy = 'category'; when using wp_list_categories().

  9. zebrastribe
    Member
    Posted 11 months ago #

    I tried:
    1.

    <?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 );
    	echo '<pre>';
    	print_r($term_ids);
    	echo '</pre>';
    
    	$terms = wp_list_categories( 'title_li=&style=none&echo=0&include=' . $term_ids );
    	$terms = rtrim( trim( str_replace( '',  $separator, $terms ) ), $separator );
    
    	// display post categories
    	echo  $terms;
    }									
    
    									?>

    and

    2.

    <?php 
    
    // 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 );
    	echo '
    <pre>';
    	print_r($term_ids);
    	echo '</pre>
    ';
    
    	$terms = wp_list_categories( 'title_li=&style=none&echo=0&include=' . $term_ids );
    	$terms = rtrim( trim( str_replace( '',  $separator, $terms ) ), $separator );
    
    	// display post categories
    	echo  $terms;
    }									
    
    									?>

    with the first piece of code it shows 7, 1, Features, Uncategorized in a list with 7,1 inside the "pre" tag without a link.
    (it is the right categories for this posts and there IDs)

    The second does not work..

  10. esmi
    Forum Moderator
    Posted 11 months ago #

    The second does not work

    In what way? what's the error?

  11. zebrastribe
    Member
    Posted 11 months ago #

    Sorry. The categories is not outputted at all and the space is left blank

  12. esmi
    Forum Moderator
    Posted 11 months ago #

    Try checking your site's error logs for messages. Your hosts should be able to help you accessing your site's error logs.

  13. zebrastribe
    Member
    Posted 11 months ago #

    Okay. I am on mamp and the php_error.log does not show an error?

  14. esmi
    Forum Moderator
    Posted 11 months ago #

    Are you sure that you looking in the right error log?

  15. zebrastribe
    Member
    Posted 11 months ago #

    Well, I think it is because it shows some errors from yesterday...

    <?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 . '&exclude=1' . '&include=' . $term_ids );
    $terms = rtrim( trim( str_replace( '<br />',  $separator, $terms ) ), $separator );
    
    // display post categories
    echo  $terms;
    										}									
    
    									?>

    And this is showing the right two categories for this specific post on index.php

  16. zebrastribe
    Member
    Posted 11 months ago #

    Can I may make the exclude on:

    $post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );

    so that the categories do not show up in the list?

  17. bcworkz
    Member
    Posted 11 months ago #

    wp_get_object_terms() does not accept "exclude" arguments, but you can use array_diff() on the returned results to remove unwanted data. This is probably a good idea, otherwise the wp_list_categories() function eventually ends up writing a SQL query that in part is something like WHERE term_id IN (1) AND term_id NOT IN (1). I'm not sure how SQL would handle this, but it's not a good query in any case.

  18. zebrastribe
    Member
    Posted 11 months ago #

    Thanx bcworkz.
    How would the new code look with the array_diff() if I used the standard code from the codex

    <?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( '',  $separator, $terms ) ), $separator );
    
    	// display post categories
    	echo  $terms;
    }
    ?>
  19. zebrastribe
    Member
    Posted 11 months ago #

    Tried this but it does not work...

    <?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( '',  $separator, $terms ) ), $separator );
    	$terms_ex = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=1' );
    	$terms_ex = rtrim( trim( str_replace( '',  $separator, $terms ) ), $separator );
    
    	// display post categories
    	$result=array_diff($terms_ex,$terms);
    	echo $result;
    }
    ?>
  20. bcworkz
    Member
    Posted 10 months ago #

    You applied array_diff() to the wrong data in the wrong place.

    Essentially, you apply array_diff() to $post_terms, the values returned from wp_get_object_terms(). It may be a bit more complicated, depending on how you come up with the exclusion array. wp_list_categories() will not work well because it returns HTML, not an array. You could parse the HTML into an array, but that's quite convoluted. You could just define an array of IDs you wish to exclude, but wp_get_object_terms() returns an array of objects, not IDs, so the two arrays are incompatible for getting a diff from them.

    I would suggest running a quick foreach loop to build an array of IDs from the $post_terms, the take a diff of that with the predefined array of exclusion IDs. There may be some spiffy array function that can extract the IDs you need that might be more slick, but I'm unaware of such a function. The loop will certainly work.

    Since you are dealing with an array here, you can continue with the original approach of imploding the array into a string to be used in wp_list_categories(), but actually, the preferred way to pass arguments is via an array. The string format is the old way and has been sort of superseded. The string arguments still work mainly for backwards compatibility reasons.

    If you convert all the arguments to array form, there'll be no need to implode the diff'd array.

    It might help you understand what's going on better (such as why you cannot diff the return from wp_list_categories()) if you print_r() some interim variables just to see what values they contain. It's also a good way of troubleshooting code when things don't work. The print_r() output is a mess when viewed in a normal browser window. View it in source view or output it inside <pre> tags so the data structure is maintained.

  21. zebrastribe
    Member
    Posted 10 months ago #

    oh my ...stupid me and my fat fingers.

    this seems to work:

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

    Is there a better way then is code?

    when I google "array_diff()" it seems that a lot of posts is about that is slow on large arrays. How large is large?

Reply

You must log in to post.

About this Topic

Tags

No tags yet.