• How To?
  • Tips ‘n Tricks
  • WordPress
  • Snippets
  • Software
    • Browsers
    • Downloads
  • Web
  • Tools
    • Character Counter
    • chmod Calculator
    • Entities Encoder
    • Live HTML Editor
    • My IP
  • Contact
Twitter Facebook Google+ RSS
You are here: SumTips » Blogging » WordPress Post, Category, Page Excerpt as Meta Description Tag

WordPress Post, Category, Page Excerpt as Meta Description Tag

Posted on April 7, 2012 by Renji | Short URL: http://sumtips.com/?p=6268

Most search engines don’t consider meta description in calculating the importance of a web page, but it’s still used to create snippets of text that users see when your web page appears in search results. The meta description of a page is found between the <head> and </head> tags. It’s coded as:

<meta name="description" content="Insert a short description of your website here." />

Most of the free WordPress themes available out there don’t have the Meta Description Tag and users have to opt for a plugin to fix this issue. In this post, I will show you a quick and simple way to optimize your WordPress site’s Meta Description using the existing ‘Excerpt’ feature. With it, WordPress will autmatically display correct (the one you set) Meta Description for each post, category, page and homepage.

Below is the complete code that you need to add to your WordPress theme. Open header.php file and insert the PHP code between <head> and </head> tags, preferbly below <title> and </title> tags. If already there’s a meta description tag, just delete it and paste this code.

<?php
if (is_single()) {
?>
<meta name="description" content="<?php echo strip_tags(get_the_excerpt($post->ID)); ?>" />
<?php
} else if( is_category() ) {
?>
<meta name="description" content="<?php echo strip_tags(category_description(get_category_by_slug(get_the_category())->term_id)); ?>" />
<?php
} else if(is_home() || is_page()) {
?>
<meta name="description" content="Insert a short description of your website here.">
<?php } ?>

So how does this code work?

WordPress simply extracts the text that you wrote in the excerpt text area of a post and assigns it as the meta description. For example, if you have a post entitled “My Birthday” and in the excerpt area you typed in “Had a rocking birthday this year!”, it’s meta description will show up like this:

<meta name="description" content="Had a rocking birthday this year!" />

Code explanation:

Post description as meta description

We are using the get_the_excerpt();function to extract text from the Excerpt field in the post editor.

WordPress Post Excerpt

WordPress Post Excerpt

By default, <p>...</p> tags are also inserted along with the excerpt. To get rid of those we use another function strip_tags();.

<?php
if (is_single()) {
?>
<meta name="description" content="<?php echo strip_tags(get_the_excerpt($post->ID)); ?>" />

If the excerpt field of a post is left blank, the meta description would also be blank. It would then show up like this in the source code:

<meta name="description" content="">

Category description as meta description

For the dynamic category page, we will use the description that you write in the Edit Category page. Rest the code works similar to the previous one.

WordPress Category Excerpt

WordPress Category Excerpt

<?php
} else if( is_category() ) {
?>
<meta name="description" content="<?php echo strip_tags(category_description(get_category_by_slug(get_the_category())->term_id)); ?>" />

Default meta description

Here, write the default meta description that you want for all other pages on your site.

<?php
} else if(is_home() || is_page()) {
?>
<meta name="description" content="Insert a short description of your website here.">

Optional:

Page description as meta desctiption

As WordPress doesn’t all us to write excerpts for pages, you can write custom descriptions for any specific page the following following way:

<?php
} else if(is_page(3)) {
?>
<meta name="description" content="Insert a short description of your website here.">

Here ’3′ is the page ID.

That’s all! If you face any problems with the codes, let me know in the comments and I will try to help you out.


Tweet

Related posts:

  • Set Different Post Templates Based on ID, Tag and Category in WordPress
  • Change "Enter title here" text for Post, Page and Custom Post Type in WordPress
  • Exclude Specific Post, Category and Author from WordPress Feed
  • Remove WordPress Meta Generator tag
  • Get Color-Coded Post Statuses in WordPress with UI Labs
Categories: Blogging | Tags: WordPress
Add Custom Header Banner Image to Plugin Page on WordPress.org
Give Your Site Visitors a Quick Print Preview Option, Ctrl+P Hotkey

One Response to “WordPress Post, Category, Page Excerpt as Meta Description Tag”

  1. Silesia says:
    April 11, 2012 at 3:46 AM

    Interesujący wpis. Myślę, że powinno się o nim dowiedzieć więcej ludzi. Nie myślałeś o dodaniu adresu strony do jakiegoś katalogu stron www?

  • Get Updates via Email

  • Recent Posts

    • CMS2CMS: Migrate Site from Drupal or Joomla to WordPress
    • WordPress: Add Preview Button in Distraction Free Writing
    • How to Activate or Deactivate Individual Jetpack Modules
    • Windows 8: Auto Update Defender with Windows Update Disabled
    • Automatically Start and Close Programs at Specific Time
    • How to Copy Code from Notepad++ with Syntax Highlighting
  • Random Posts

    • Custom Maintenance Page Template For WordPress
    • Google's Zeitgeist 2010 Review Video
    • Unsubscribe from Twitter Weekly Digest
    • Facebook Allows Page Name Change & Tests New Page Admin Interface
    • How to Permanently Delete a Facebook Group?
    • Sync User Folders, Wallpapers, Sticky Notes & More Across Computers
  • Categories

    • Blogging
    • Games
    • Google
    • How To?
    • Linux
    • Microsoft
      • Windows
    • Miscellaneous
    • Phone
    • Snippets
      • AutoHotkey Snippets
      • CSS Snippets
      • htaccess Snippets
      • JavaScript Snippets
      • PowerShell Snippets
      • WordPress Snippets
    • Social
    • Software
      • Browsers
    • Tips 'n Tricks
    • Wallpapers
    • Web
© SumTips. Contact | Sitemap | Privacy Policy