如何显示自定义帖子中的类别信息
2 个回答
- 投票数
-
- 2010-11-19
要获取此特定帖子的分类术语,那么您需要的是
wp_get_post_terms($post->ID, 'yourtaxonomyname')
这将返回指定分类中指定帖子的术语数组.该法典页面为: http://codex.wordpress.org/Function_Reference/wp_get_post_terms
如果您使用的是分类法get_term($taxonomy_name,$term_id)中的特定术语.您还可以使用get_terms()
获取分类法的所有术语这里是使用方法的示例.
$terms = wp_get_post_terms($post->ID,'toolkit'); foreach ($terms as $term) { echo $term->description; }
To get the taxonomy term for this particular post, then what you need is
wp_get_post_terms($post->ID, 'yourtaxonomyname')
This will return an array of terms in the specified taxonomy for the post specified. The codex page is: http://codex.wordpress.org/Function_Reference/wp_get_post_terms
If you're after a specific term in a taxonomy get_term($taxonomy_name, $term_id). You can also get all terms for a taxonomy using get_terms()
Here's an example of how to use it.
$terms = wp_get_post_terms($post->ID,'toolkit'); foreach ($terms as $term) { echo $term->description; }
-
其实我现在很困惑.您是否需要将常规类别分类法附加到自定义帖子,还是只需要输出每个类别的描述(其中类别是指普通WP类别分类法中的项目)?Actually, I'm confused now. Do you need the normal category taxonomy to be attached to custom posts, or do you just need to output descriptions for each of your categories (where category means an item in the normal WP category taxonomy)?
- 0
- 2010-11-19
- anu
-
对不起,令人困惑!我需要为自定义帖子创建的类别.因此,仅是这些职位的分类法.那有意义吗?Sorry to be confusing! I need the categories I created for the custom posts. So, it's a taxonomy just for these posts. Does that make sense?
- 0
- 2010-11-19
- Jeff Tancil
-
陷阱-使用我认为您需要的内容来编辑我的答案gotcha - editing my answer with what I think you need
- 0
- 2010-11-19
- anu
-
谢谢,阿努抱歉成为phpnitwit,但是我尝试了一下却一无所获:''我应该将其保留为' ID,'toolkit','description')?>'?Thanks, Anu. Sorry to be a php nitwit, but I tried this and got nothing: '' should I have left it as 'ID, 'toolkit', 'description') ?>' ?
- 0
- 2010-11-21
- Jeff Tancil
-
添加了一个快速的代码片段Added a quick code fragment
- 0
- 2010-11-21
- anu
-
- 2010-11-19
让您将数字类别ID 传递到category_description函数中.
如果您期望的是,变量$ category的值不会自动填充.您需要为输出的每个类别设置它.
如果您发布相关代码(您用于循环访问该类别列表的循环),则更容易进行故障排除.
Make you're passing the numeric category id into the category_description function.
The value of the variable $category won't fill itself automatically, if that's what you were expecting. You need to set it for each category you output.
If you post the relevant code (the loop you're using to sipt out that category list), it'd be easier to troubleshoot.
-
谢谢,MathSmath,我会尝试的.我当时只是个小人物!代码在上方,再次感谢您.Thanks, MathSmath, I will try that. I was being a tad literal minded! Code is above, thank you again.
- 0
- 2010-11-19
- Jeff Tancil
-
MathSmath,我添加了以下内容,但仍然没有得到猫的描述.我在做蠢事,对不对?`'portfolio','toolkit'=>'preparation')); //循环从这里开始 if(have_posts()):while(have_posts()):the_post(); ?> `MathSmath, I added the following and still don't get the cat description. I am doing something dumb, right? ` 'portfolio', 'toolkit' => 'preparation' ) ); //the loop start here if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> `
- 0
- 2010-11-19
- Jeff Tancil
-
杰夫,请在您的原始问题中添加循环代码,因为这样阅读起来会更好.Jeff, please add the loop's code inside your original question as it is much better to read then.
- 0
- 2010-11-19
- hakre
-
我认为category_description()仅适用于默认类别分类法-不适用于自定义分类法I think category_description() only works for the default category taxonomy - not a custom taxonomy
- 0
- 2010-11-21
- anu
我创建了一个使用自定义帖子的页面: http://www.africanhealthleadership.org/resources/toolkit/
每个工具(准备,评估等)都是自定义帖子.在WP Admin上,每个工具都是一个类别.每个类别都有一个"描述"字段.我想在"工具包"页面上输出这些描述.我尝试使用此功能,但未显示任何内容:
<?php echo category_description( $category ); ?>
现在,描述已硬编码到页面中.准备开始 "准备工具正在建立..."
谢谢您的任何想法! 杰夫
以下是循环显示自定义帖子类型的循环:
这是来自functions.php的代码