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?
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.
the_archive_title( string $before = '', string $after = '' ) Displays the archive title based on the queried object.
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.
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.”
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With