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 titlesingle_cat_title()
: to get current category title. There’re sibling functions for tags and terms as well:single_tag_title()
andsingle_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 usePost 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 to get the current page URL:
global $wp;
$link = add_query_arg( $wp->query_string, '', home_url( $wp->request ) );
$text = wp_title( '|', false );
printf(
'<iframe allowtransparency="true" frameborder="0" scrolling="no" src="https://platform.twitter.com/widgets/tweet_button.html?url=%s&text=%s&count=horizontal"></iframe>',
urlencode( $link ),
rawurlencode( $text )
);
WordPress Developers Source Link
By default, the page title will display the separator before the page title, so that the blog title will be before the page title. This is not good for title display, since the blog title shows up on most tabs and not what is important, which is the page that the user is looking at.
There are also SEO benefits to having the blog title after or to the ‘right’ of the page title. However, it is mostly common sense to have the blog title to the right with most browsers supporting tabs. You can achieve this by using the seplocation parameter and setting the value to ‘right’. This change was introduced around 2.5.0, in case backward compatibility of themes is important.
Plugins might use the wp_title filter to generate a value. While it is possible to construct a “title” by doing things such as concatenating with bloginfo
(the Site Name), if you do not use the wp_title
function in your theme, you will likely have errors or unexpected behavior.