Unique meta description and meta keyword tags in your WordPress themes
Meta description tag: update
Here is the meta description PHP with both category and tag pages included (and updated again to make them more than one word and to include monthly archives):
<meta name="description" content=
"<?php
if(is_home()) {echo ('Malcolm Coles: His blog for his reviews, so you can find him, and other stuff that takes his fancy.');}
else {if(is_category()) {echo category_description();}
else {if(is_tag()) {echo '-tag archive page for this blog' . single_tag_title();}
else {if(is_month()) {echo ' archive page for this blog' . the_time('F, Y');}
else {echo get_post_meta($post->ID, "Metadescription", true);}}}}?>">
I've given up adding keywords these days! Remember, as below, you need to add a custom field called Metadescription to get this to work.
Original post about meta descriptions and keyword tags
I know little about PHP. But after fiddling round with other people's example code, I've finally managed to use custom fields to get a unique meta description and unique meta keywords on each post, and on the home page.
To get this to work, you need to add new custom fields called Metadescription and Metakeywords to each post. The ones for the home page you hard code. And I'm still working on the category pages ...
Copy and paste the code below into the head section of the header bit of your wordpress template. It basically checks if it’s the home page. If it is, it uses the hard-coded meta description and meta keywords. If it’s not, it goes and looks up the custom fields and uses the values from there. So there we have it: a unique meta description and unique meta keywords in wordpress.
Unique meta descriptions in wordpress
<meta name="description" content="<?php if(is_home()) {echo ('PUT YOUR HOME PAGE DESCRIPTION HERE');} else {echo get_post_meta($post->ID, "Metadescription", true);}?>">
Unique meta keywords in wordpress
<meta name="keywords" content="<?php if(is_home()) {echo ('PUT YOUR HOME PAGE KEYWORDS HERE');} else {echo get_post_meta($post->ID, "Metakeywords", true);}?>">
You might also like
- Adding page numbers: How to avoid duplicate titles and meta descriptions in pages 2 and higher of WordPress category and tag pages
- Ignore WordPress’s SEO advice
- Avoid duplicate content with paged comments in wordpress
- WordPress comment pagination & duplicate content
- Meta keywords tag: Bing says carry on but Google says it’s a waste of time – but
Some hacking there
but you don't have to jump through the hoops. Several plugins (eg: All In One SEO) offer this functionality and more without the hacking.
[...] know that the default for most WordPress themes is to NOT include a META description? Why you ask? I found this blog to help sort this out and I agree with what Malcolm says. If you have ever built your blog from [...]
Thanks for posting mate and commenting on the headmeta plugin site. I was after something simple that didn't add "another plugin" to WP, something easily manageable, and this is it.
[...] goes to Malcolm Coles for this awesome recipe! Did I helped you? If yes, feel free to make a small [...]
[...] VÃa | Malcolm Coles [...]
I've only recently started using WordPress but I was a little surprised to see that the meta description tag wasn't included as default. I suppose it could have taken the first paragraph or X characters as the text for the description but I can imagine this could create some pretty useless descriptions and you would prefer something more focused.
I too like to dabble and have taken a similar route to yourself and added the tag in the code manually based on the page title functionality in themes/default/archive.php. I have nothing against plugins but this seemed too simple a request to require one.
I suppose I'll have to be careful when it comes time to upgrade though but then again a plugin that works with the current version may not work after an upgrade either.
Thanks, this is a great post... I improve lil bit to add meta description on tag archive, here the code:
<?php if(is_tag()) {
echo ''; } else {}
?>
=)
Your PHP seems to have been eaten... But no worries as my code above already generates a meta description for tag pages.
Great codes! Thank you very much Malcolm Coles
Отличный поÑÑ‚, прочитав неÑколько Ñтатей на Ñту тему понÑл, что вÑÑ‘ таки не поÑмотрел Ñ Ð´Ñ€ÑƒÐ³Ð¾Ð¹ Ñтороны, а поÑÑ‚ как-то очень заинтереÑовал.
Courtesy of http://uk.babelfish.yahoo.com/translate_txt, this means "Outstanding post, after reading several articles on this theme he understood that all after all did not look from the other side, but post somehow greatly interested." Apparently.
well, it did say that. When I switched host, all non-european characters got mangled. Something to do with unicode I imagine. So now it looks like gibberish
I actually have the All-In-One SEO plugin installed. However, although I find it works incredibly well (and is available) for adding and editing POSTS, the plugin is nowhere to be found when I add or edit a PAGE. Does anyone know why this is, or what can be done about it? If the directions above are the only solution to this problem, then sobeit, I suppose, but if there's a way to make it work like it's supposed to, I'd like to try it.
I am using version 1.4.6.11 of the SEO Pack, and version 2.7.1 of WordPress. The blog address is http://www.principia-college-football.org.
Any ideas? Anyone?
[...] Read More … [...]
[...] Unique meta description and meta keyword tags in your WordPress themes [...]
[...] Unique meta description and meta keyword tags in your WordPress themes [...]
[...] Unique meta description and meta keyword tags in your WordPress themes [...]
[...] Unique meta description and meta keyword tags in your WordPress themes [...]
[...] Unique meta description and meta keyword tags in your WordPress themes [...]
[...] Unique meta description and meta keyword tags in your WordPress themes [...]
[...] Unique meta description and meta keyword tags in your WordPress themes [...]
Thanks for this post!
I had figured I needed to use those custom meta fields to add the keywords and description to each page, but I just couldn't figure out the code to add to the templates.
Well, you're using all in one SEO pack, so you could probably just use that ... I'll give you the benefit of the doubt and let you keep the link
I have been searching for this code all day yesterday and finally figured out the phrase to put in the search engine and voila - up pops your site! Your instructions worked perfectly on all three of my sites! I wonder why all the theme developers don't include keywords anymore when some search engine tools still look for them.
Thank you so much! Hopefully now people can find me on search engines.
I thought it were a myth that some search engine tools are still looking for meta keywords. But I just learnt I were wrong about that. So I am really glad, I found this code. Just trying it. Works perfect. Thanks alot to Malcolm!
BTW: It might be a bit of work to add new custom fields to each and every post. However, I prefer it that way. As the source code of the page looks much more tidy than it would be if I were using "all in SEO" (or similar plugins).
Great little piece of code. works great! and I prefer not using plugins.
It works on every site I have on my server using WP.
However, I manage a WP site on a diff server & it will not work. this site has the same theme & plugins as another site that it is working on. odd. has to be a server issue?
Claire - I can't see what difference a server would make. Are you sure you've not missed out a quote mark or pointy bracket (or forgotten to set a custom field)?
A quick follow-up. knowing I use this code on many other WP sites - I dug deeper.
Turns out a domain had not been redirected as I though it was by the client. once I got the domain issue figured out - no problems.
this is by far the best little piece of code better than using a plugin anyday.
thank you Malcom!
Thanks Malcom for this nifty piece of code. As a newbie to the world of html, php and css I am truly grateful to people like you who share their knowledge so freely and generously.
Thank you!
A silly question...
So you past the code into the header.php file in the field, but is it best to be at the beginning of the field, or just before the closing declaration?
Not certain as to whether there is any significant difference or not, but simply wanted to clarify.
@scott I usually paste the code right under the title tag. not sure if where int he header makes any difference
@Claire
Thanks
Fantastic code!
Just a quick question. I am trying to get this to work with specific categories but my code just creates a white page. What might I b doing wrong?
Here's my code: http://paste-it.net/public/k1a1bcd/
Any pointers greatly appreciated.
Thanks,
Karl
Karl - I have to say I'm not a PHP expert - took me a lot of trial and effort to get this to work!
But if you look at my code, the tag line is like this:
else {if(is_tag()) {echo '-tag archive page for this blog' . single_tag_title();}
whereas yours is
elseif(is_tag()) { echo '-tag archive page for this Ely Herald' . single_tag_title();}
The beginning is different - mine says:
else {if(is_
whereas yours is:
elseif(is_
So you're missing a space and also my { bracket has become a ( bracket in your code. At the very end I have a bunch of closing }}}}s - one for each if statement. You don't.
Maybe try changing it so these things are different?
I am having great difficulty...not with your code, but difficulty in combining your code with existing code.
THE ISSUE: I have a meta tag that defines specific post descriptions; however my homepage does not currentlty display the blog description because there isnt a home page per se, only an index holder for a custom home page display.
So, the so called homepage doesnt display a description, it displays the description of the most recent post.
I am hoping to use your code in combination to allow ONLY my home page to display the blog description, and then if its a post page to display the custom SEO description instead.
So, how can we combine your code with the code I have to make this work?
Here is my current code:
ID, "_seo_description", true) ): ?>
<meta name="description" content="" />
** My last comment didnt display the whole code...and i removed all of the opening tags
?php if( get_post_meta($post->ID, "_seo_description", true) ): ?
meta name="description" content="
?php echo get_custom_field('_seo_description'); ?"
?php else: ?
?php endif; ?
still not displaying... please see my source code at my website.
Am using your suggestions and can't wait to see if this changes (helps) my page rankings. It's a real pita to figure this stuff out, even with all of the plugins - lots of competing interests and strategies.
Great post. It helped get me on the right path to a solution. Here's the code I ended up with to eliminate the duplicate meta tags Google was flagging when it crawled page 2, page 3, etc of my main index page. This code appends "| page 2" etc to the keywords and description meta tags when navigating to older entries of main index page.
<?php if(is_home()) {
echo '<meta name="keywords" content="YOUR KEYWORDS GO HERE';
if ( $paged ';
}
?>
<?php if(is_home()) {
echo '<meta name="YOUR DESCRIPTION GOES HERE';
if ( $paged ';
}
?>