Tim Yang’s Weblog

31/7/2023

Howto: Customising the description metatag to the title of each post in Wordpress

Filed under: Howto, Wordpress — Tim Yang @ 12:00 am

Instead of having a set of standard metatags across all the pages of my blog, I wanted to customise the description metatag of each post to the title of the post and the keywords metatag to the categories of each post. I thought I saw a plugin that did this, but when I looked again, I couldn’t find it. Because I’m reading up on Wordpress Template Tags right now, it made sense for me to try do something different with them. So out comes the PHP for Dummies manual. I succeeded (partially) in my goal. If you check the description metatag of each post, they are all customised while the homepage has the standard blog name and description that’s set in the admin interface. But I failed in the categories as keywords because the category template tags don’t work outside of the Loop.

Here’s the code I used. Just copy the description metatag part and paste it between your head tag to achieve the result.

	<meta name="description" content="<?php if ( is_single() ) {
		single_post_title('', true);
	} else {
		bloginfo('name'); echo " - "; bloginfo('description');
	}
	?>" />

1 Comment

  1. Good and simple tip.
    Thanks for this.

    Comment by Rildo Moraes — 26/8/2023 @ 9:02 am

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress