Support » Plugins » Hacks » Exclude categoies from wp_list_categories?

  • 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?

Viewing 15 replies - 1 through 15 (of 20 total)
  • esmi

    (@esmi)

    Forum Moderator

    Are you trying to exclude category 1?

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

    esmi

    (@esmi)

    Forum Moderator

    What happens if you just use:

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

    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…

    esmi

    (@esmi)

    Forum Moderator

    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?

    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

    esmi

    (@esmi)

    Forum Moderator

    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().

    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..

    esmi

    (@esmi)

    Forum Moderator

    The second does not work

    In what way? what’s the error?

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

    esmi

    (@esmi)

    Forum Moderator

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

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

    esmi

    (@esmi)

    Forum Moderator

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

    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

    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?

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘Exclude categoies from wp_list_categories?’ is closed to new replies.