Optimize your WordPress theme for Search Engines – Part 2


Today I’m showing how to add your meta descripition to your blog. Google will use this as the description that shows up in it’s search results which can help with click through if you write good excerpts about your posts. If you don’t have a specific excerpt written then wordpress automatically grabs the first 55 words which may or may not be very descriptive of your post. It’s always a good practice to manually write your excerpt to give a summary of a post.

First I’ll post the code that needs to go into your header.php file between the <head> </head> then I’ll explain the pieces and what the do

<?php if (is_single() || is_page() ) : if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<meta name="description" content="<?php $excerpt = strip_tags(get_the_excerpt());
        echo $excerpt; if ( $cpage < 1 ) {}
else { echo (' - comment page '); echo ($cpage);} ?>" />
<?php endwhile; endif; elseif(is_home()) : ?>
<meta name="description" content="<?php bloginfo('description'); ?>" />
<?php endif; ?>

The if (is_single() || is_page() ) determines if the current page is a single post or a single page. Next is this if ( have_posts() ) : while ( have_posts() ) : the_post(); this is mini loop to pull in the excerpt for the current post or page. Next this code pulls the excerpt and strips any html tags from it to display a clean excerpt $excerpt = strip_tags(get_the_excerpt()); echo $excerpt;. After that is the comment page code again to elimanate duplicate meta tags with google if ( $cpage < 1 ) {} else { echo (' - comment page '); echo ($cpage);}. The last bit of code is if it's just the home page then the tagline from the blog is displayed. That's it for section. Tomorrow I'll go over how to add your tags and categories to the meta keywords, some say search engines don't pay much attention to these but it doesn't hurt to have them. You can follow the whole series here. Be sure and rate each post, thanks.

About Jeremy Clark

Small town IT worker with interests in all things technological and technical. Biggest interests are in web development especially the WordPress publishing platform and the community supporting it. Currently developing and maintain the free WordPress theme Techozoic. I'm also always available for hire.
This entry was posted in wordpress and tagged , , . Bookmark the permalink.

3 Responses to "Optimize your WordPress theme for Search Engines – Part 2"

Leave a reply

Comments will be sent to the moderation queue.