A simple way to use the excerpt to generate a dynamic meta description on your posts and pages in Wordpress
It’s a quick tip but one that’s potentially very useful and means you don’t have to mess around with the all in one SEO pack plugin each time you post something.
I always use the Excerpt field when posting, it allows me to create a small intro description for a post for when people are browsing the archives/categories.
Because I don’t want the hassle of writing a new meta description for each article, I figured you should be able to just use the excerpt code in the site header to recreate your short description so each post or page on your site then has a nice SEO friendly description in the head of your document which is what will be displayed in search engines when people find your posts.
My initial thought would be to drop the normal
<?php the_excerpt(); ?> tag directly into the head of my template to match it up but unfortunately this actually outputs your meta description wrapped in a paragraph tag and is not quite as practical as I’d hoped. (see source code screenshot)
However, with a bit of searching around, I located this snippet of code which can be dropped into the header.php file just beneath the <title> tags in your template which outputs the excerpt used in your entries RSS feed instead. This means that you can have automatically generated meta descriptions for your posts without having to add in extra detail each time you post.
The code to use for dynamic meta descriptions
<?php if (is_single() || is_page() ) : if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <meta name="description" content="<?php the_excerpt_rss(); ?>" /> <?php endwhile; endif; elseif(is_home()) : ?> <meta name="description" content="<?php bloginfo('description'); ?>" /> <?php endif; ?>
Success! Dynamic meta data
Hope this proves of use to those of you who don’t want to mess around with the all in one SEO pack on every post!