php 从单个帖子获取 Wordpress 类别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4289879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:32:49  来源:igfitidea点击:

Get Wordpress Category from Single Post

phpwordpressthemeswordpress-theming

提问by Ian

I'm finishing up a WP theme, and I'm on the single.php template. I'm having some issues because I need to access the parent category that a post is in in order to display certain images and XML content.

我正在完成一个 WP 主题,我正在使用 single.php 模板。我遇到了一些问题,因为我需要访问帖子所在的父类别才能显示某些图像和 XML 内容。

Here is an example of what I'm talking about. The following is the end url of a single post:

这是我正在谈论的一个例子。以下是单个帖子的结束网址:

/andrew/leaf-art-2/

/安德鲁/叶艺术-2/

/andrew/is the category, and leaf-art-2is the single post. When I am on the single post, I am having trouble getting single_cat_title();to return the category that the current post is in. I am using single_cat_title();instead of the_category();because it displays the string value of the category which I then use to place a picture of the artist (whose category this is) on their posts. I don't have any use for the url, I just need the string with the category name.

/andrew/是类别,leaf-art-2是单个帖子。当我在单个帖子上时,我无法single_cat_title();返回当前帖子所在的类别。我使用single_cat_title();而不是the_category();因为它显示类别的字符串值,然后我用它来放置艺术家的照片(这是他们的类别)在他们的帖子上。我对 url 没有任何用处,我只需要带有类别名称的字符串。

Any good ways of doing this? I have been searching the Wordpress Codex and lots of forums, and haven't found any answers yet.

有什么好的方法可以做到这一点?我一直在搜索 Wordpress Codex 和许多论坛,但还没有找到任何答案。



The following was my original post.

以下是我的原帖。

I have set up a category called "artists" which when I run single_cat_title("", false);I can get the string value of the category and then use it to search for the appropriate artist image using XML.

我已经设置了一个名为“艺术家”的类别,当我运行它时,我single_cat_title("", false);可以获得该类别的字符串值,然后使用它来使用 XML 搜索适当的艺术家图像。

This works fine on the category.php template page.

这在 category.php 模板页面上工作正常。

The problem is that when I'm actually inside of a single post that has the "artists" category, single_cat_title(); doesn't output any information to the page, thereby keeping me from accessing the XML data.

问题是,当我实际上在具有“艺术家”类别的单个帖子中时, single_cat_title(); 不会向页面输出任何信息,从而使我无法访问 XML 数据。

I need to, while in the "artists" > "sample" post, be able to get from WP the category.

我需要在“艺术家”>“样本”帖子中,能够从 WP 获得该类别。

P.S. the above category is one of many that is using this setup, which is why I can't hardcode it.

PS 上述类别是使用此设置的众多类别之一,这就是我无法对其进行硬编码的原因。

回答by Chris

How about get_the_category?

get_the_category怎么

You can then do

然后你可以做

$category = get_the_category();
$firstCategory = $category[0]->cat_name;

回答by Sjoerd

For the lazy and the learning, to put it into your theme, Rfvgyhn's full code

懒人和学习,把它放到你的主题里,Rfvgyhn的完整代码

<?php $category = get_the_category();
$firstCategory = $category[0]->cat_name; echo $firstCategory;?>