在自定义帖子类型中显示当前分类法术语
4 个回答
- 投票数
-
- 2013-03-02
好,所以我终于在这里找到了我需要的东西: 如何在我的自定义中获取当前术语WordPress中的分类法?
由@ user3208底部提供的最新更新:
<?php // Get terms for post $terms = get_the_terms( $post->ID , 'oil' ); // Loop over each item since it's an array if ( $terms != null ){ foreach( $terms as $term ) { // Print the name method from $term which is an OBJECT print $term->slug ; // Get rid of the other data stored in the object, since it's not needed unset($term); } } ?>
那解决了我的问题! 谢谢
Ok, so I finally found what I needed here: How to get current term in my custom taxonomy in WordPress?
the last update at the bottom courtesy of @user3208:
<?php // Get terms for post $terms = get_the_terms( $post->ID , 'oil' ); // Loop over each item since it's an array if ( $terms != null ){ foreach( $terms as $term ) { // Print the name method from $term which is an OBJECT print $term->slug ; // Get rid of the other data stored in the object, since it's not needed unset($term); } } ?>
That solved my issue! Thanks
-
- 2013-03-01
您应该改用
wp_get_post_terms
.$terms = wp_get_post_terms( $post_id, $taxonomy, $args );
get_terms
将为您提供分类法中存在的所有术语更新:
global $post; $terms = wp_get_post_terms( $post->ID, 'genre'); print_r($terms); #displays the output
You should use
wp_get_post_terms
instead.$terms = wp_get_post_terms( $post_id, $taxonomy, $args );
get_terms
will give you all the terms present in a taxonomy.UPDATE:
global $post; $terms = wp_get_post_terms( $post->ID, 'genre'); print_r($terms); #displays the output
-
我正在尝试,但是没有用.我必须将任何变量传递给函数吗?你能指定我应该如何在我的代码中实现它吗?谢谢I am trying but it's not working. do I have to pass any vars into the function? can you specify how should I implement it in my code? Thanks
- 0
- 2013-03-01
- gil hamer
-
如果您位于WordPress循环中,则可以使用get_the_ID()代替$post_id.对于`$taxonomy`,您需要添加您正在使用的分类法的名称.`$ args`不是必需的.If you are in the WordPress Loop, you can use `get_the_ID()` instead of `$post_id`. For `$taxonomy`, you need to add the name of the taxonomy you're using. `$args` isn't necessary.
- 0
- 2013-03-01
- RRikesh
-
绝对不在循环之内!只是不能让它工作..你能建议如何在循环外实现它吗?如果需要,我将发布整个代码.谢谢It's definitely outside the loop! just cant get it to work.. can you suggest how to implement it outside the loop? if necassary I will post the whole code. Thanks
- 0
- 2013-03-01
- gil hamer
-
然后,您需要添加`global $post;`,然后使用`$post-> ID`获取帖子ID.Then you need to add `global $post;` and then use `$post->ID` to get the post ID.
- 0
- 2013-03-01
- RRikesh
-
您可以使用上面的代码向我展示示例吗?我不是那个程序员.我会感激的.谢谢can you show me an example using my code above? I am not that programmer. I'll appreciate it. Thanks
- 0
- 2013-03-01
- gil hamer
-
更新了答案.updated the answer.
- 0
- 2013-03-01
- RRikesh
-
不幸的是,它没有用:1.它给了我一种艺术Unfortunately it didn't work: 1. It gives me an art
- 0
- 2013-03-01
- gil hamer
-
抱歉,它对我不起作用:1.给我一个错误消息,显示不必要的数组(可能您不能忽略'$ arg'参数.2.当我显示另一个术语的帖子时,它没有更改术语名称还有其他解决方案吗?Sorry but it didn't work for me: 1. it gives me an error displaying unnecessary array (probably you cant ignore the '$arg' parameter. 2. it didn't change the term name when I displayed a post from another term. any other solution? Thanks
- 0
- 2013-03-01
- gil hamer
-
- 2016-10-28
采用user3208编码的内容,我添加了一些代码,将URL添加到术语中.希望能帮助到别人.
<?php // Get terms for post $terms = get_the_terms( $post->ID , 'oil' ); // Loop over each item since it's an array if ( $terms != null ){ foreach( $terms as $term ) { $term_link = get_term_link( $term, 'oil' ); // Print the name and URL echo '<a href="' . $term_link . '">' . $term->name . '</a>'; // Get rid of the other data stored in the object, since it's not needed unset($term); } } ?>
Taking what user3208 coded, I have added a bit of code that adds the URL to the Term. Hope that helps someone out.
<?php // Get terms for post $terms = get_the_terms( $post->ID , 'oil' ); // Loop over each item since it's an array if ( $terms != null ){ foreach( $terms as $term ) { $term_link = get_term_link( $term, 'oil' ); // Print the name and URL echo '<a href="' . $term_link . '">' . $term->name . '</a>'; // Get rid of the other data stored in the object, since it's not needed unset($term); } } ?>
-
- 2017-07-09
<?php echo get_the_term_list( $post->ID, 'yourtaxonomy', '', ', ' ); ?>
<?php echo get_the_term_list( $post->ID, 'yourtaxonomy', '', ', ' ); ?>
-
通常仅回答代码的答案而没有解释.您能否请[编辑您的答案](https://wordpress.stackexchange.com/posts/272817/edit)并解释此功能的作用以及如何解决原始问题,也许可以链接至食品法典以获取更多信息?Code only answers are usually frowned upon without an explanation. Could you please [edit your answer](https://wordpress.stackexchange.com/posts/272817/edit) and explain what this function does and how this would solve the original problem, maybe linking to The Codex for more information?
- 0
- 2017-07-10
- Howdy_McGee
嗯,这应该很简单,但是我在网络上的任何地方都找不到答案.我发现的所有答案都很接近,但不完全是我所需要的. 我需要的是仅显示我所在的自定义帖子类型的当前字词. 并非所有术语只是一个!(相关的一个)
这是我正在使用的内容,但是它显示了所有不利于我的条款:
记住-我想在我的单个帖子类型模板中显示它 有人可以建议吗? 谢谢