Aleš Sýkora wrote

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

26. 1. 2021 in ,

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

5 comments on “Get WordPress title for page, post, category, archive in Oxygen”

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

    Thanks!

    1. 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.

Leave a Reply to Mike Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

More from category
Aleš Sýkora wrote
Oxygen Front-end styles in Gutenberg
WARNING: I can’t figure how to make it working currently. If you have any idea, please put in in the comments below. You may want to display the content when editing in Gutenberg editor with your frontend Oxygen Styles included. It is possible with But the challenge is that Oxygen is creating CSS files for […]
Read More
Aleš Sýkora wrote
Oxygen Pro Menu menu selector – workaround fix on multisite
9. 8. 2021 in ,
Symptoms: Pro menu on multisite doesn’t show the WordPress menus in select field. Unfortunately, this is a known issue when using the Pro Menu element on a multisite install. There is a workaround by manually adding the ID of the menu you want to display to the page or template shortcodes. To do this, add […]
Read More
Aleš Sýkora wrote
Display list of WooCommerce categories
19. 2. 2021 in
If you need to display list of all WooCommerce categories without products, you can use my code. It will display all categories with link to their archive page. It will also display category image, name of course and count of products inside the category. It is all wrapped in HTML with OxyNinja framework applied. So […]
Read More
magnifiercross linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram