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

时间:2013-08-26 08:20:35

标签: wordpress categories archive

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

5 个答案:

答案 0 :(得分:36)

使用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
)

希望它有所帮助!

答案 1 :(得分:3)

检查此代码是否有帮助??

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;

答案 2 :(得分:1)

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

www.sitename.com/category/design

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

single_cat_title();

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

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

在h1标签中显示“设计”类别。我希望这有助于某人。

答案 3 :(得分:0)

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

答案 4 :(得分:0)

简单是最好的

//Code 2
var x = 10;
var y = 10;
while (x != 1010 && y != 1010) {
    console.log(x, y);
    var circle = new Path.Circle(new Point(x, y), 10);
    circle.fillColor = 'orange';

    // increment x every time
    x += 100;

    // then check if it will break the loop
    if (x == 1010) {
        x = 10;
        y += 100;
    }
}