wordpress 在archive.php 页面Wordpress 上获取自定义类别名称或ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18439673/
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-09-08 19:19:53  来源:igfitidea点击:

Get custom category name or id on archive.php page Wordpress

wordpresscategoriesarchive

提问by user2688562

How can I "fetch" custom category name or id on archive.php page. So when i'm on that page template, how can i know which custom category posts are showing?

如何在 archive.php 页面上“获取”自定义类别名称或 ID。因此,当我在该页面模板上时,我如何知道正在显示哪些自定义类别帖子?

回答by iEmanuele

Use get_queried_object();to retrieve the currently-queried object.

使用get_queried_object();检索当前查询的对象。

In a taxonomy term case:

在分类术语情况下:

//Custom taxonomy is project_type, custom term is web-design
$obj = get_queried_object();

echo '<pre>';
print_r( $obj );
echo '</pre>';

Displays the following:

显示以下内容:

stdClass Object
(
    [term_id] => 56
    [name] => Web Design
    [slug] => web-design
    [term_group] => 0
    [term_taxonomy_id] => 56
    [taxonomy] => project_type
    [description] => Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    [parent] => 0
    [count] => 0
)

Hope it helps!

希望能帮助到你!

回答by Shiva

Check if this code helps ??

检查此代码是否有帮助?

if ( is_single() ) {
$cats =  get_the_category();
$cat = $cats[0]; // let's just assume the post has one category
}
else { // category archives
$cat = get_category( get_query_var( 'cat' ) );
}
$cat_id = $cat->cat_ID;
$cat_name = $cat->name;
$cat_slug = $cat->slug;

回答by Bernard Myburgh

If you are trying to display the current category on your archive.php page, for instance, if your url is:

如果您尝试在您的 archive.php 页面上显示当前类别,例如,如果您的 url 是:

www.sitename.com/category/design

Then to echo 'design' you would use the following:

然后要回显“设计”,您将使用以下内容:

single_cat_title();

On my archive page I have the following:

在我的存档页面上,我有以下内容:

<h1 class="page-heading"><?php single_cat_title(); ?></h1>

That displays the category 'design' in an h1 tag. I hope this helps someone.

在 h1 标签中显示类别“设计”。我希望这可以帮助别人。

回答by Firefog

The simple is the best

简单的才是最好的

$term_id       =    get_queried_object_id();
$prod_term        =    get_term($term_id,'product_cat');
$current_cat_slug =    $prod_term->slug;    

回答by Debakant Mohanty

Try the this piece of code to get the category name!! $cat_name = single_cat_title('',false);

试试这段代码来获取类别名称!!$cat_name = single_cat_title('',false);