只显示帖子选定的分类术语?

发布于 2024-11-26 17:01:42 字数 234 浏览 5 评论 0原文

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

我是否正在尝试做一些无法完成的事情?我确信这就是我眼前的东西。我只是想在对我的做事方式做出重大改变之前询问专业人士。

When using wp_get_post_terms() I can produce a list of taxonomy terms associated with a post. However, I only want to show the taxonomy terms that have been selected for that post. Using the aforementioned function and get_terms() will successfully find the taxonomy terms, but it will show all of the terms. Not only the ones that have been selected. In the $args array for the functions I've looked for a 'selected' filter, but I found none and when I tried it, it didn't work.

Am I trying to do something that can't be done? I'm sure it's something that is starring me right in the face. I just want to ask the pro's before I make major changes to the way I'm doing things.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

通知家属抬走 2024-12-03 17:01:42

wp_get_post_terms 仅返回为该帖子选择的术语,不会返回所有分类术语。

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

wp_get_post_terms only returns terms that have been selected for that post, it doesn't return all taxonomy terms.

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

耀眼的星火 2024-12-03 17:01:42
<?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> ';
} 
?>
<?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();
?>

This works well for me. I simply send the taxonomy slug to browser, and itterate through them with the code above.

I send by this:

<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> ';
} 
?>
呆萌少年 2024-12-03 17:01:42

你可以试试这个代码,它对我有用。我有一个名为“商店”的分类法,我想显示从中选择的 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>';
?>

You can try out this code, it worked for me. I have a taxonomy named 'stores' and i wanted to display 2 selected taxonomy from it. so i used a include feature.

<?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>';
?>
三生路 2024-12-03 17:01:42
<?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) ,''; ?>
<?php echo get_the_term_list( $post->ID, 'your_taxonamy'); ?>

And if you want it without the term linking you can use this

<?php $terms_as_text = get_the_term_list( $post->ID,'your_taxonamy'); if (!empty($terms_as_text)) echo '', strip_tags($terms_as_text) ,''; ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文