Home > Dev > WordPress SEO > Dynamic Meta Descriptions

Dynamic Meta Descriptions

This code dynamically generates meta descriptions for posts and pages.

// Add this to your theme's functions.php file

function dynamic_meta_description() {
    global $post;
    if (is_single() || is_page()) {
        $description = strip_tags($post->post_excerpt ? $post->post_excerpt : $post->post_content);
        $description = substr($description, 0, 155);
        echo '<meta name="description" content="' . esc_attr($description) . '">';
    }
}
add_action('wp_head', 'dynamic_meta_description');
Back