Tag Archives: themes

Title Template Tags in WordPress

WordPress themers should be aware of all the different title template tags out there for properly calling page titles.  Not every function will work in all situations, so you have to think carefully about what to use and where.

Below is my synopsis of all the title template tags that WordPress has to offer:

Template Tag Proper Usage
wp_title() Used to display the page title within the HTML <title> tag.  This is typically altered for SEO purposes and should not be used to display page titles in the content area of a page.
the_title() Used within the loop to display the title of a post or page.  This function outputs the title immediately and is a wrapper function for get_the_title().
get_the_title() Used to fetch the title of a post or page.  Can be used outside the loop if is_singular() is true.  This is the only function on our list that doesn’t automatically print out the title to the page.  You can optionally provide an ID and get the page title for another page as well.
single_post_title() Used outside the loop to display the title for a single post or page.  This function also works when trying to display the title for the posts page when a user’s settings have designated a static page to display their blog.
single_cat_title() Used outside the loop on category pages to display the title for WordPress’s built-in categories.  It is now just a wrapper function for single_term_title() and will probably be depreciated in the future.
single_tag_title() Used outside the loop on tag pages to display the title for WordPress’s built-in tags. It is now just a wrapper function for single_term_title() and will probably be depreciated in the future.
single_term_title() Used outside the loop on taxonomy pages to display the title for any taxonomy.  This means that this function can be used instead of single_cat_title() and single_tag_title() and will also work for any custom taxonomies that have been defined.
single_month_title() Used outside the loop to display the title for date based archives.  The title will display the month and year and will require you to assign a prefix in order for it to appear correctly.  If a user is on a year based archive page, only the year will appear in the title.
post_type_archive_title() Used outside the loop to display the title on the main archives page (aka posts page) for custom post types ONLY.
the_search_query() Used outside the loop to display the search query.  Great to use in your search.php theme file, if you have one.

I would love to hear if and how you have been using these in your themes.

Get the title for the posts page in WordPress

This little tidbit is for all you WordPress themers out there.  It is important that your theme displays the proper title on the page no matter what settings a user has on their site.  As you probably are already aware, a user can go to ‘Settings’ -> ‘Reading’ in the WordPress admin menu and change what pages are used for the front page and the posts page (aka blog page).

Set Blog Page in WordPress

So in the example above, I want to set a static page as my homepage and delegate another page to host my blog (aka posts page).  So now that my posts page is no longer the front page, I want to display the page title that the user assigned to that page in my theme.  This helps add clarity for users and makes all of my pages have a more consistent appearance.  As an example, it could be that I am using the posts page for displaying news items and want to label my posts page ‘News’.

Rather than leaving it up to the user to go in and try to hack your theme to get the page title to appear, you decide you want to display this page title for them automatically.

WordPress provides you with the the_title() function, which works great inside the loop.  Problem is, you are using that inside the loop to display the title for all of your blog entries on the page and that won’t do you any good when trying to fetch the page title outside the loop.  If you try to use this function for the page title outside the loop, all you get is the title of the first post on the page.

So how do we fetch the page title for our posts page?

There are two ways to do it.  First is the easy way:

single_post_title();

The only problem you might encounter with this method is that the function echos our title immediately.  If you want to get the title as a variable for any reason, you would have to use output buffering to do it.

You can use the get_the_title() function to get the title of a page as a variable just by providing the ID.  If you don’t provide an ID, then the function will try to fetch the ID for the current page.  Unfortunately, this function doesn’t detect the current page ID properly in our use case, which is why using the_title() function didn’t work for us earlier:  the_title() is just a wrapper function for get_the_title().

Luckily for us, WordPress does store the ID of the page you want to use for the posts page in the database.  So we can fetch the title as a variable, like this:

$our_title = get_the_title( get_option('page_for_posts', true) );

This may be more than you ever needed to know about fetching the title for an assigned posts page, but now you know! 😉

Choosing a WordPress Theme

Choosing a WordPress theme can be a confusing task for a lot of people.  Everyone can choose a theme that looks nice, but being able to head off potential problems before you launch a theme on a live site is extremely important.  As a WordPress developer, I am often asked to review WordPress themes for clients.  In order to avoid major issues, I recommend doing the following checks on any theme that you are seriously considering.  These checks are listed in the order that they should be done:

  1. Date Last Updated – If you are looking at free themes in the WordPress theme repository, you can easily check this by clicking on the theme you are interested in and checking the ‘Last Updated’ date in the right-hand column.  You really want to find a theme that has been updated relatively recently.  The idea here is that you don’t want to have to hire someone to make a lot of changes to be sure it is compatible with the most recent version of WordPress.  If you are buying a premium theme, you will want to be sure that you will have access to support and upgrades.

  2. Test Thoroughly – You can demo most themes before you download them, whether they are free themes or paid.  If you can’t demo a paid theme before you download it, I would recommend looking elsewhere.  Make sure you don’t just look at the homepage on the demo; visit all the possible links and watch out for issues.  Once you decide to download the theme, perform the same checks you did in the demo and make sure that all the functionality that you are expecting is available in the admin area.  This will require that you have some content on the site.  If the theme doesn’t take advantage of features like the new customized menus, or doesn’t support widgets, this would be a deal-breaker for most people.  If you really like the theme, you can have a WordPress developer fix these things for you.  You may also want to consider how easily you can insert your logo.

  3. Code Quality – Run your theme through a markup validation service to see how it checks out.  If you don’t mind touching code, disable any WordPress plugins and make sure the theme you want to check is active.  In your wp-config.php file, found in the root directory that you installed WordPress in, drop in this line of code: define(WP_DEBUG, true);.  You will now be able to see any errors with the PHP code in the theme.  Hopefully, there won’t be any.  Poor code simply means more issues that will have to be fixed.  Watch out for themes that try to obfuscate the code so you can’t change it.  It is important that you are able to make changes as needed.

  4. Cross-Browser Compatibility – Your visitors may be viewing your site using Internet Explorer, FireFox, Safari, Google Chrome, Opera or another web browser.  Regardless of the browser or version being used, you still want them to be able to see and use the site.  Adobe BrowserLab is currently a free service that will allow you to see how your site will look in different browsers.

This list applies to any type of theme, not just WordPress.  Even if you are having a web developer create a custom theme for you, be sure to personally do checks 2-4!