WordPress 4.1 is bringing with it a couple of cool new additions for theme developers. They’re things that are currently a bit messy to implement in themes. For me they are things that I do the same way in all my themes and so there is a lot of duplication – as such it’s nice to see them added. Below I will go over what has changed – and I will describe how to make use of the updates.
add_theme_support( ‘title-tag’ );
The title tag has been added as a theme feature and implementing it is easy. All you have to do is delete thetag and the reference to wp_title from your themes header.php. You should also remove (or test) any filters you have running on the wp_title hook. For me this is a great little improvement. The current method of adding the wp_title call and then filtering the output so that it is actually nice has always felt a bit messy so having it all added to a single location makes the theme a lot cleaner and means that the function can be maintained by WordPress itself.
Of course filters still work on the output and so plugins like WP SEO can still filter the output according to your requirements.
To add this to your own theme just add the following to your themes functions.php:
function bm_title_tag() {
// title tag
add_theme_support( 'title-tag' );
}
add_action( 'after_setup_theme', 'bm_title_tag' );
the_pagination()
Numeric pagination is great for users- it gives a nice quick way to skip through long archives of content. However WordPress has not previously had a native way to add pagination to posts. I wrote a blog post last year about one method of adding pagination using native WordPress functions – but I’ve since found out it’s not perfect so a native method that wraps up that complexity is very welcome.
Basic usage is simple but there’s also a bunch of parameters you can use to edit how the links are output. The basic parameters seem to be the same as for the existing paginate_links function – but they make it’s usage a lot simpler.
the_archive_title()
There are lots of types of Archives in WordPress. You can list posts by date (year, day, month), category, tag, taxonomy, and more. For most people each archive will look the same, but it will need a different title format. The normal method for managing this is to create a huge if then statement with a lot of conditions – but that’s a mess, and isn’t easily filterable. Now the code is a single function call – the_archive_title()
.
If you want to wrap the title in header tags (or other html) then pass them as parameters – eg:
the_archive_title( '<h1 class="page-title">', '</h1>' );
the_archive_description()
I get the impression that not many people use the category description functionality – but I think it’s a really nice thing to include in a theme and to use on a website. It gives some context to categories and adds additional text to category pages which can help with SEO.
Usage is similar to the_archive_title above – however it replaces less code than the previous function. In my case it only removes a couple of lines – but it’s still nice to have standard methods for these things.
the_archive_description( '<div class="taxonomy-description">', '</div>' );
Wrapping Up
I think it’s good to see some of these common issues being simplified and standardized. Anything that removes code from my themes is good and reduces the burden on theme authors.
To keep up to date with WordPress development you can follow the make core blog – it can be technical but I find I often pick up intereting nuggets like the functions above. To save sifting through loads and loads of content it can be helpful to just look at the week in core summaries. For themes in particular you can also follow the incredibly active _s project on Github.
Was it good/ useful/ a load of old rubbish? Let me know on Mastodon, or BlueSky (or Twitter X if you must).
Link to this page
Thanks for reading. I'd really appreciate it if you'd link to this page if you mention it in your newsletter or on your blog.