【发布时间】:2013-08-28 16:19:00
【问题描述】:
如何在archive.php 页面上“获取”自定义类别名称或ID。那么当我在那个页面模板上时,我怎么知道显示了哪些自定义类别的帖子?
【问题讨论】:
-
对于“自定义类别名称”,您是指添加到自定义分类的术语还是添加到标准类别的术语?
-
我的意思是自定义分类术语
标签: wordpress categories archive
如何在archive.php 页面上“获取”自定义类别名称或ID。那么当我在那个页面模板上时,我怎么知道显示了哪些自定义类别的帖子?
【问题讨论】:
标签: wordpress categories archive
检查此代码是否有帮助??
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;
【讨论】:
使用get_queried_object(); 检索当前查询的对象。
在分类术语的情况下:
//Custom taxonomy is project_type, custom term is web-design
$obj = get_queried_object();
echo '<pre>';
print_r( $obj );
echo '</pre>';
显示以下内容:
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
)
希望对你有帮助!
【讨论】:
试试这段代码获取类别名称!! $cat_name = single_cat_title('',false);
【讨论】:
如果您尝试在您的archive.php 页面上显示当前类别,例如,如果您的网址是:
www.sitename.com/category/design
然后,要回显“设计”,您将使用以下内容:
single_cat_title();
在我的存档页面上,我有以下内容:
<h1 class="page-heading"><?php single_cat_title(); ?></h1>
在 h1 标记中显示类别“设计”。我希望这对某人有所帮助。
【讨论】:
简单的就是最好的
$term_id = get_queried_object_id();
$prod_term = get_term($term_id,'product_cat');
$current_cat_slug = $prod_term->slug;
【讨论】: