Search code examples
wordpresscategoriesarchive

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


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?


Solution

  • Use get_queried_object(); to retrieve the currently-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!