Are you trying to exclude category 1?
Yes. I also need to specify more categories later. Maybe as a variable $exclude_cat?
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…
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
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..
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
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?
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?