在类别模板中获取类别ID
8 个回答
- 投票数
-
- 2011-05-29
$wp_query->get_queried_object()
将为您提供"当前查询的对象".在类别档案库中,这是类别对象,在作者页面中,这是作者,在单个帖子上,这是帖子本身,...好了,您就明白了.如果只想要ID,也可以使用$wp_query->get_queried_object_id()
.$wp_query->get_queried_object()
will give you the "currently queried object". On a category archive this is the category object, on a author page this is the author, on a single post this is the post itself, ... well, you get the the idea. If you only want the ID you can also use$wp_query->get_queried_object_id()
. -
- 2017-02-04
根据我的搜索,您必须使用此:
$category = get_queried_object(); echo $category->term_id;
base on my search you must use this:
$category = get_queried_object(); echo $category->term_id;
-
我假设您收到了否决票,因为您不_解释_为什么会有所帮助,仅说明这样做了.您可能需要检查该功能的来源,并将结果作为编辑添加到答案中.提示:如果评论需要比您的答案更长,那么它应该是评论:)I assume you received the downvote because you are not _explaining_ why it helps, just stating that this does. You might want to inspect the source of that function and add your outcomings as edit to your answer. Hint: If a comment needs to be longer than your answer, then it should have been a comment :)
- 0
- 2017-02-04
- kaiser
-
- 2011-07-01
嗯,我还不能发表评论,但是VicePrez的回答确实有用.以下内容在类别存档页面上可以正常工作(尽管您可能只想做一些回声):
<?php $category = get_the_category(); echo $category[0]->cat_ID; ?>
编辑:抓紧一点,它对我有用,直到我遇到一个没有帖子的类别,然后它选择了子类别而不是主要类别.您不能依赖类别模板页面上的get_the_category .
Umm, I can't comment yet, but VicePrez's answer does work. The following works just fine on a category archive page (although you probably want to do something other than just echo it):
<?php $category = get_the_category(); echo $category[0]->cat_ID; ?>
EDIT: Scratch that, it worked for me until I came across a category that didn't have a post, then it picked up the subcategory instead of the main category. You can't rely on get_the_category on a category template page.
-
这不是可行的解决方案,因为它会返回该类别页面上所有帖子的所有类别.您回显的结果将返回列表中最新帖子的初始类别.This is not a viable solution as this returns all the categories for all the posts on that category page. Your echoed result returns the initial category of the latest post in the list.
- 0
- 2017-11-21
- cj5
-
- 2011-05-29
除非我对这个问题有误解,我认为您也可以将类别ID/段塞添加到主体类中:
<?php if(is_category()) { $cat_ID = 'cat-'.get_query_var('cat'); } ?> <body <?php body_class($cat_ID); ?>>
Unless I am misunderstanding the question, I think you can also add the category id/slug to the body class:
<?php if(is_category()) { $cat_ID = 'cat-'.get_query_var('cat'); } ?> <body <?php body_class($cat_ID); ?>>
-
- 2011-07-12
@Jan Fabry的回答实际上是正确的答案,原因如下:由于Wordpress允许帖子的多个类别,因此使用
$category = get_the_category()
并查询$category[0]
在每种情况下都无法正常工作,因为您实际上在要求第一篇文章的第一类.想象一下,您有A,B和C类别.如果您只有一个帖子,则有A和B类别,并且您位于B的类别页面中,那么您可能最终得到A的信息.这就是为什么最好使用
$category = $wp_query->get_queried_object()
,因为在上一个示例中,当您进入B的类别页面时,它总是可以获取B的信息./p>@Jan Fabry's response is actually the correct answer, here's why: Since Wordpress allows multiple categories for a post, using
$category = get_the_category()
and querying$category[0]
will not work in every case since what you're actually doing is asking for the first category of the first post. Imagine you have categories A, B and C. If you have only one post, it has categories A and B and you're inside B's category page, you may end up with A's information instead.That's why it's better to use
$category = $wp_query->get_queried_object()
, because in the previous example it will always get you B's information when you're inside B's category page. -
- 2011-05-29
您可以使用
get_the_category()
来做到这一点.示例:
<?php $category = get_the_category(); // use this to echo the slug echo $category[0]->slug; // use this to echo the cat id echo $category[0]->cat_ID; // if you've got multiple categories you can run a foreach loop like so foreach ( $category as $cat ) : echo '<li>' . $cat->name . '</li>'; endforeach; ?>
您可以使用:
<?php echo '<pre>'; print_r($category); echo '</pre>'; ?>
查看返回的对象数组.
You could use
get_the_category()
to do that.Example:
<?php $category = get_the_category(); // use this to echo the slug echo $category[0]->slug; // use this to echo the cat id echo $category[0]->cat_ID; // if you've got multiple categories you can run a foreach loop like so foreach ( $category as $cat ) : echo '<li>' . $cat->name . '</li>'; endforeach; ?>
You could use:
<?php echo '<pre>'; print_r($category); echo '</pre>'; ?>
to view the array of objects that are returned.
-
"如何在模板中获取猫的ID"这个问题有不同的解释.在单个帖子模板中,您是对的.它在循环内工作以获取单个帖子的类别;但是,`get_the_category()`在类别存档页面中无法获取类别ID,结果将是任意的.the question 'how do I get the cat ID inside the Template' is open to different interpretation. within a single post template, you are right. it works inside the loop to get categories of a single post; however,`get_the_category()` will not work in a category archive page to get the category id, the result would be arbitrary.
- 0
- 2011-05-29
- Michael
-
@迈克尔真实地说.@Jan似乎已针对该特定上下文给出了更适当的答案.@Michael true say. @Jan seems to have given a more appropriate answer in relation to that specific context.
- 0
- 2011-05-29
- VicePrez
-
`get_the_category()`确实在category.php中起作用`get_the_category()` does work inside category.php
- 1
- 2011-07-05
- Lea Cohen
-
- 2013-01-11
$category = get_category( get_query_var( 'cat' ) ); $cat_id = $category->cat_ID; $catname=explode(",",get_category_parents($cat_id,'',',')); print_r($catname);
$category = get_category( get_query_var( 'cat' ) ); $cat_id = $category->cat_ID; $catname=explode(",",get_category_parents($cat_id,'',',')); print_r($catname);
-
请解释"为什么"可以解决问题.代码段不是答案.Please explain **why** that could solve the problem. A code snippet is not an answer.
- 1
- 2013-01-11
- fuxia
-
- 2013-09-23
上面的大多数示例都可以使用,但是如果您使用多个类别,则NONE (截至WP 3.6版,撰写本文时)的其他方法可以用来获取已传递给任一"猫"或"类别名称".
我发现的唯一方法是从以下位置提取数据:
$wp_query->query['category_name']
由于某种原因,这会向
get_query_var( 'category_name' )
返回不同的值,该值仅返回第一个类别.使用多个类别时,您将必须使用诸如
explode
之类的功能来获取类别子弹数组,然后循环遍历以获取所有ID:<?php global $wp_query; //grab all categories from query string (if using `category_name`) $category_slugs_array = explode("+",esc_attr($wp_query->query['category_name'])); $categories = array(); $category_ids = array(); //loop through all the slugs foreach($category_slugs_array as $category_slug) { //get category object using slug $category = get_category_by_slug( $category_slug ); //check to make sure a matching category has been found if(isset($category->cat_ID)) { $categories[] = $category; $category_ids[] = $category->cat_ID; } } var_dump($categories); //array of categories var_dump($category_ids); //array of category IDs ?>
使用AND(+)或OR(,)运算符时,显然需要考虑一些因素.
Most of the above examples work but if you are using multiple categories NONE (as of writing, WP version 3.6) of the other methods work to get all the categories that have been passed to either "cat" or "category_name".
The only way I have found is to pull the data from:
$wp_query->query['category_name']
For some reason this returns a different value to
get_query_var( 'category_name' )
which only returns the first category.When using multiple categories you will have to use some function like
explode
to get an array of category slugs, then loop through that to grab all the IDs:<?php global $wp_query; //grab all categories from query string (if using `category_name`) $category_slugs_array = explode("+",esc_attr($wp_query->query['category_name'])); $categories = array(); $category_ids = array(); //loop through all the slugs foreach($category_slugs_array as $category_slug) { //get category object using slug $category = get_category_by_slug( $category_slug ); //check to make sure a matching category has been found if(isset($category->cat_ID)) { $categories[] = $category; $category_ids[] = $category->cat_ID; } } var_dump($categories); //array of categories var_dump($category_ids); //array of category IDs ?>
Obviously there needs to be some considerations when using AND (+) or OR (,) operators.
如何在模板中获取猫的ID.很重要: 我不能用这个名字来做,因为我们有多个同名的猫科动物.只有the不同.如果我没事,那也可以. 但是就像我说的那样:我不能使用Cat标题.....