Aleš Sýkora / November 28, 2023 / 5 comments

Get WordPress title for page, post, category, archive in Oxygen

Post summary: Want to create only one template for Header and Footer – the main template – in Oxygen sometimes? So You need to display the title of current page, post, archive, category in one place. It cannot be done with Oxygen conditionals :(. So you have to make it done with code block and custom PHP.…

Want to create only one template for Header and Footer – the main template – in Oxygen sometimes? So You need to display the title of current page, post, archive, category in one place. It cannot be done with Oxygen conditionals :(. So you have to make it done with code block and custom PHP.

We will use this functions:

is_category()
is_tag()
is_author()
is_tax()
is_post_type_archive()
is_home()
is_page()
is_single()
is_search()
is_404()

Use this code in Code Block

<?php 
if ( is_category() ) {
 //CATEGORY
		$title = single_cat_title( '', false );
	} elseif ( is_tag() ) {
 //TAG
		$title = single_tag_title( '', false );
	} elseif ( is_author() ) {
 //AUTHOR
		$title = '<span class="vcard">' . get_the_author() . '</span>';
	} elseif ( is_post_type_archive() ) {
 //POST ARCHIVE
		$title = post_type_archive_title( '', false );
	} elseif ( is_tax() ) {
 //TAXONOMY
		$title = single_term_title( '', false );
	} elseif ( is_home() ) {
 //HOMEPAGE
		$title = single_post_title( '', false );
	}  elseif ( is_page() ) {
 //PAGE
		$title = single_post_title( '', false );
	} elseif ( is_single() ) {
 //POST
		$title = single_post_title( '', false );
	} elseif ( is_search() ) {
 //SEARCH
		$title = "Search";
	} elseif ( is_404() ) {
 //404
		$title = "404 not found";
	} 
?>
<h1 class="responsive-h1"><?php echo $title; ?></h1>

Put it in the code block and style with your custom classes.. I am using hte .responsive-h1 class.

Code block with dynamic Title

Fuel my passion for writing with a beer🍺

Your support not only makes me drunk but also greatly motivates me to continue creating content that helps. Cheers to more discoveries and shared success. 🍻

5 comments

  • Thank you. That was I looking for!

  • The new edited version of snippet doesnt work man :(

  • Can you please also add the php code for the title for:
    – Search results page, and
    – 404 Error page

    Thanks!

  • A

    It works for me. I have updated the code, but I forgot the quotes, I’m sorry. Can you try it now?

  • A

    Hello Mike! I have Updated the code with this:

    elseif ( is_search() ) {
    //SEARCH
    $title = "Search";
    } elseif ( is_404() ) {
    //404
    $title = "404 not found";
    }

    Hopefully it help you.

Share Your Thoughts