[英]Excluding terms (category names) by ID for current post
I found this function to display the terms attached to a post but i can't manage to find a way to exclude specific ID's of some category terms i don't want to display in the list. 我发现此功能可以显示帖子中附带的术语,但是我无法找到一种方法来排除我不想在列表中显示的某些类别术语的特定ID。 Could someone give me a clue were to start?
有人可以给我一个线索开始吗? I looked up all the functions used in this function but can't seem to find arguments to exclude id's.
我查找了此函数中使用的所有函数,但似乎找不到用于排除id的参数。
Thanks in advance! 提前致谢!
// get taxonomies terms links
function custom_taxonomies_terms_links() {
global $post, $post_id;
// get post by post id
$post = &get_post($post->ID);
// get post type by post
$post_type = $post->post_type;
// get post type taxonomies
$taxonomies = get_object_taxonomies($post_type);
$out = "<ul>";
foreach ($taxonomies as $taxonomy) {
// get the terms related to post
$terms = get_the_terms( $post->ID, $taxonomy );
if ( !empty( $terms ) ) {
foreach ( $terms as $term )
$out .= '<li><a href="' .get_term_link($term->slug, $taxonomy) .'">'.$term->name.'</a></li> ';
}
}
$out .= "</ul>";
return $out;
}
Add another conditional if
statement within your second foreach()
to check whether or not the $term
should be ignored. 在第二个
foreach()
添加另一个条件if
语句,以检查$term
是否应被忽略。 For example: 例如:
// An array of IDs to ignore/exclude
$excluded_ids = array( 1, 2, 3, 4);
foreach ( $terms as $term ) {
// Only proceed if the term_id is NOT in the $excluded_ids array
if ( !in_array( $term->term_id, $excluded_ids ) ) {
$out .= '<li><a href="' .get_term_link($term->slug, $taxonomy) .'">'.$term->name.'</a></li> ';
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.