使用 wp_get_post_terms() 时,我可以生成与帖子相关的分类术语列表。但是,我只想显示为该帖子选择的分类术语。使用上述函数和 get_terms() 将成功找到分类术语,但它会显示所有术语。不仅是那些被选中的。在函数的 $args 数组中,我寻找了一个“选定”过滤器,但我没有找到,当我尝试它时,它不起作用。
我是否正在尝试做一些无法做到的事情?我敢肯定,这件事让我眼前一亮。在我对我做事的方式做出重大改变之前,我只想问问专业人士。
wp_get_post_terms
仅返回已为该帖子选择的术语,它不会返回所有分类术语。
http://codex.wordpress.org/Function_Reference/wp_get_post_terms
<?php
$the_selected = $_GET['cat'];
$args = array( 'post_type' => 'portfolio_item', 'posts_per_page' => 11, 'orderby' => 'id', 'order' => 'DESC', 'themes_categories' => "$the_selected");
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
这对我很有效。我只是将分类学 slug 发送到浏览器,并使用上面的代码遍历它们。
我通过这个发送:
<li>Filter By:</li>
<?php
$categories=get_categories($args);
foreach($categories as $category) {
echo '<li><a href="' . get_category_link( $category->term_id ) . '?cat=' . $category->slug.'" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </li> ';
}
?>
你可以试试这段代码,它对我有用。我有一个名为“商店”的分类法,我想从中显示 2 个选定的分类法。所以我使用了包含功能。
<?php
$taxonomy = 'stores';
$args1=array(
'include'=> array(12,30)
);
$terms = get_terms('stores',$args1 );
echo '<ul>';
foreach ($terms as $term) {
//Always check if it's an error before continuing. get_term_link() can be finicky sometimes
$term_link = get_term_link( $term, 'stores' );
if( is_wp_error( $term_link ) )
continue;
//We successfully got a link. Print it out.
echo '<li><a href="' . $term_link . '">' . $term->name . '</a></li>';
}
echo '</ul>';
?>
<?php echo get_the_term_list( $post->ID, 'your_taxonamy'); ?>
如果你想要它没有链接这个词,你可以使用它
<?php $terms_as_text = get_the_term_list( $post->ID,'your_taxonamy'); if (!empty($terms_as_text)) echo '', strip_tags($terms_as_text) ,''; ?>