WordPress获取栏目名称

文章描述:

WordPress获取当前栏目信息和栏目名称

 

栏目

当前栏目名称和信息

if(is_category()) {
    $cat = get_query_var('cat');
    $yourcat = get_category($cat);
    echo "<pre>";
    print_r($yourcat);

    echo "该分类别名为" . $yourcat->slug;
}

结果:

WP_Term Object
(
    [term_id] => 1
    [name] => 问答
    [slug] => ask
    [term_group] => 0
    [term_taxonomy_id] => 1
    [taxonomy] => category
    [description] => 
    [parent] => 0
    [count] => 1
    [filter] => raw
    [cat_ID] => 1
    [category_count] => 1
    [category_description] => 
    [cat_name] => 问答
    [category_nicename] => ask
    [category_parent] => 0
)
该分类别名为ask

页面

当前页面名称和信息

if( is_page() ) {
    $content = $content . get_option('display_copyright_text');
    $post_data = get_post($post->ID, ARRAY_A);
    echo "<pre>";
    print_r($post_data);
    echo $slug = $post_data['post_name'];
}

结果:

Array
(
    [ID] => 2
    [post_author] => 1
    [post_date] => 2022-04-11 13:50:43
    [post_date_gmt] => 2022-04-11 05:50:43
    [post_content] =>

 

    [post_title] => 示例页面
    [post_excerpt] => 
    [post_status] => publish
    [comment_status] => closed
    [ping_status] => open
    [post_password] => 
    [post_name] => sample-page
    [to_ping] => 
    [pinged] => 
    [post_modified] => 2022-04-11 15:24:21
    [post_modified_gmt] => 2022-04-11 07:24:21
    [post_content_filtered] => 
    [post_parent] => 0
    [guid] => http://localhost:10004/?page_id=2
    [menu_order] => 0
    [post_type] => page
    [post_mime_type] => 
    [comment_count] => 0
    [filter] => raw
    [ancestors] => Array
        (
        )

    [page_template] => default
    [post_category] => Array
        (
        )

    [tags_input] => Array
        (
        )

)
sample-page

end

发布时间:2022/04/11

发表评论