仅显示帖子选择的分类术语?

时间:2011-07-29 08:22:26

标签: wordpress taxonomy posts

使用wp_get_post_terms()时,我可以生成与帖子相关的分类术语列表。但是,我只想显示为该帖子选择的分类术语。使用上述函数和get_terms()将成功找到分类术语,但它将显示所有术语。不仅是被选中的。在函数的$ args数组中,我寻找了一个“选定的”过滤器,但我没有找到,当我尝试它时,它没有用。

我是否尝试做一些无法做到的事情?我确信这是我面对面的主演。在我对自己的工作方式做出重大改变之前,我只想问专业人士。

4 个答案:

答案 0 :(得分:1)

wp_get_post_terms仅返回为该帖子选择的字词,但不返回所有分类字词。

http://codex.wordpress.org/Function_Reference/wp_get_post_terms

答案 1 :(得分:0)

<?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();
?>

这适合我。我只是将分类标本发送到浏览器,并使用上面的代码通过它们进行迭代。

我发这个:

<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 :(得分:0)

您可以尝试使用此代码,它对我有用。我有一个名为'stores'的分类法,我想从中显示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>';
?>

答案 3 :(得分:0)

<?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) ,''; ?>
相关问题