How To Get Current Page Title In WordPress

Sometimes we want to get current page title, which is displayed in <title> tag. Here I mean the page in general meaning, not WordPress “page”. We have several WordPress functions that can help such as:

  • get_the_title(): to get current post/page/any post type title
  • single_cat_title(): to get current category title. There’re sibling functions for tags and terms as well: single_tag_title() and single_term_title()
  • get_bloginfo(): to get the blog name
  • etc.

Using those functions are great, but they have some disadvantages:

  • Those functions are specific for particular pages like single post/page, category/tag, … pages. But we can have many page types in WordPress like: 404 page, search result page, archive page, or even virtual page.
  • The title displayed differently in themes: you may use a theme that have title in format Post Title | Blog Name, but another theme can use Post Title only. You can make a combination of the functions above to achieve the correct title, but that won’t be used widely for other themes.
  • Or you might set the title in various ways with SEO plugins

In this post, I’m gonna to use a simple trick that can solve these problems quickly.

I’m sure all of us are familiar with function wp_title(), we often use this function in header.php file, like this:

<title><?php wp_title( '|', true, 'right' ); ?></title>

to display the page title (you should use only this function to display page title, otherwise your site maybe not compatible with SEO plugins or causes performance issue).

The trick is switch the 2nd parameter $echo from true to false to get the value of current page title:

$title = wp_title( '|', false, 'right' );

Here is an example where this technique is useful. We’re going to display a Twitter button. I’m gonna to use another cool technique from Konstantin Kovshenin to get the current page URL:

global $wp;
$link = add_query_arg( $wp->query_string, '', home_url( $wp->request ) );
$text = wp_title( '|', false );
    '<iframe allowtransparency="true" frameborder="0" scrolling="no" src=""></iframe>',
    urlencode( $link ),
    rawurlencode( $text )

I’m currently use the script above (with some additions for facebook like, google+ buttons) for my websites. And it’s working great in archive pages where built in WordPress functions aren’t very helpful.

4 thoughts on “How To Get Current Page Title In WordPress

  1. That was a nice tutorial. BTW is there any way I can add code like php or java scripts inside post body just like you did without any plugin ??

    Or which plugin you use to do that ?

    1. You can use Google prettify or Syntax highlighter for WordPress. There are lots of syntax highlighting plugins like this. I think Rilwis use on of them.

  2. Никак не могу понять, как получить название собственно поста (записи)?

Comments are closed.


Subscribe to our newsletter to receive news and tutorials about WordPress and web development.

Meta Box - Create advanced WordPress custom fields and custom meta boxes with ease Download Now