maybe this is of any help:
<?php
$terms = get_the_terms( $post->ID, 'cooking-category' );
foreach ( $terms as $term ) {
if($term->term_id != 18) { // the term ID you want to exclude
$term_IDs[] = $term->term_id;
}
}
$terms = implode(',', $term_IDs);
?>
<?php wp_list_categories('taxonomy=cooking-category&include='.$terms); ?>
Thank you so much! While this worked perfectly to exclude a term from the list of terms, when attached to a post, it still displayed all the terms (whether the post was checked with this term or not) and only excluded the term I set in the function. For normal posts, in the past I have used:
<?php the_category(', ') ?>
to display only the categories that are associated with a specific post. Is it possible for the wp list categories function to display only the categories that are associated with the post? And not all the taxonomy terms?
Thanks again for your help.
try it with this:
<?php
$terms = get_the_terms( $post->ID, 'cooking-category' );
if(!empty($terms)) : ?>
<?php
foreach ( $terms as $term ) {
if($term->term_id != 18) { // the term ID you want to exclude
$term_IDs[] = $term->term_id;
}
}
$terms = implode(',', $term_IDs);
?>
<?php if($terms != '') : ?>
<ul>
<?php wp_list_categories('taxonomy=cooking-category&include='.$terms); ?>
</ul>
<?php endif; ?>
<?php endif; ?>
Thanks for this 2nd code – it is still doing the same thing as it was earlier. The term is being excluded, but all posts are showing the same terms.
I did notice that the term categories are reflecting the terms I’ve checked in the top (or latest) post. When I changed the term categories for the top post, all the term categories changed to show the same thing….
Thanks.
can you paste the full code of your template into a http://wordpress.pastebin.com/ and post the link to it here?
get_the_terms( $post->ID, 'cooking-category' ); is supposed to get only the terms of the post – http://codex.ww.wp.xz.cn/Function_Reference/get_the_terms
Sure! Here is the pastebin link with the cooking custom post type template code: http://wordpress.pastebin.com/Qef3NYBT
I’m currently using this code: get_the_terms( $post->ID, 'cooking-category' ); successfully…I just want to be able to “exclude a post term” from showing on the custom post type blog page if I don’t want it to show on the page even though I still have it categorized. How can I use the code above still: <?php echo get_the_term_list( $post->ID, 'cooking-category', ' ', ', ', '' ); ?> and just exclude a term if I want to exclude one term from displaying on the page? Thanks again for your help.
I changed the code a little bit. Put this in your theme’s function.php:
function my_get_the_term_list( $id = 0, $taxonomy, $before = '', $sep = '', $after = '', $exclude = array() ) {
$terms = get_the_terms( $id, $taxonomy );
if ( is_wp_error( $terms ) )
return $terms;
if ( empty( $terms ) )
return false;
foreach ( $terms as $term ) {
if(!in_array($term->term_id,$exclude)) {
$link = get_term_link( $term, $taxonomy );
if ( is_wp_error( $link ) )
return $link;
$term_links[] = '<a href="' . $link . '" rel="tag">' . $term->name . '</a>';
}
}
$term_links = apply_filters( "term_links-$taxonomy", $term_links );
return $before . join( $sep, $term_links ) . $after;
}
And change your:
<p>Posted in <?php echo get_the_term_list( $post->ID, 'cooking-category', ' ', ', ', '' ); ?> By <?php the_author(); ?></p>
with:
<p>Posted in <?php echo my_get_the_term_list( $post->ID, 'cooking-category', '', ' ', '', array(18) ); ?> By <?php the_author(); ?></p>
Change the number in array(18) to the term ID you want to exclude.
If you want to exclude multiple term ID’s you can populate the array with the excluded ID’s array(18,19,42)
That did it!! It worked perfectly. 🙂 Thank you so much for your help with this solution, much appreciated.
Thanks so much @keesiemeijer this works perfect for me too!
This looks like it works, but when I tried using it I got an error: “Warning: join() [function.join]: Invalid arguments…” This error only displays where the catID that I’m trying to exclude is used.