Automating META Description Tags (2 posts)

  1. BenW2811
    Posted 5 years ago #

    Is there anyway that I can automate the generation of META description tags? For example it automatically, unless I tell it differently, uses the first 160 characters of each post?

    Thanks in advance for your help.

  2. Michael
    Part-Time Forum Moderator
    Posted 5 years ago #

    you could try and add some code like this to header.php:

    <meta name="description" content="<?php
    if (have_posts()) : while (have_posts()) : the_post();
    $excerp = get_the_excerpt();
    $excerp = strip_tags($excerp);
    $excerp = preg_replace('/... &raquo; read more &raquo;/', ' - ', $excerp);
    if(!is_singular()) { $excerp = substr($excerp, 0, 160) ; }
    echo get_the_title().' : '.$excerp .' - ';
    endwhile; endif; wp_reset_query();
    " />

    this would echo:
    - the full excerpt in single post and page,
    - a 160 character shortend excerpt on index and other pages,
    in the meta description tag.

Topic Closed

This topic has been closed to new replies.

About this Topic