Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 366
user2688562 Avatar asked Aug 26 '13 08:08

user2688562


People also ask

How do I get the category name on my WordPress archive?

Most WordPress themes will automatically display the category description on the category archive pages. However, if your theme does not display category descriptions on archive pages, then you will have to modify your theme. The safest way to do this is to create a child theme.

Which function return category name on archive php page in WordPress?

the_archive_title( string $before = '', string $after = '' ) Displays the archive title based on the queried object.

How can I get current category name in WordPress?

Get Current Category ID$category = get_queried_object(); echo $category->term_id; Just place that code in any template file where a category has been queried, such as category archive pages, and you will be able to get the category id no problem.

How do I display custom taxonomy categories in WordPress?

When you add a custom taxonomy to a WordPress theme, you can display its content using one of WordPress' taxonomy theme templates. taxonomy-{taxonomy}-{slug}. php We could use this to create a theme template for a particular location, such as taxonomy-location-boston. php for the term “boston.”


1 Answers

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!

like image 147
iEmanuele Avatar answered Sep 29 '22 22:09

iEmanuele