Automatically generate a meta description on the separate ad page

On the separate ad page:

function hivepress_meta_description() {
    if (is_singular('hp_listing')) {
        $post_id = get_the_ID();
        $post = get_post($post_id);
        $description = $post->post_content;
      
        // Trim the description to 160 characters
        $description = mb_substr($description, 0, 160);
      
        echo '<meta name="description" content="' . esc_attr($description) . '">';
    }
}
add_action('wp_head', 'hivepress_meta_description', 10);

And this code will automatically generate a meta description on all pages:

function hivepress_meta_description() {
        $post_id = get_the_ID();
        $post = get_post($post_id);
        $description = $post->post_content;
      
        // Trim the description to 160 characters
        $description = mb_substr($description, 0, 160);
      
        echo '<meta name="description" content="' . esc_attr($description) . '">';
    }
add_action('wp_head', 'hivepress_meta_description', 10);

Hi,

Thanks for sharing the solution with the community; I think it will benefit many.

Where should these codes be inserted?

Hi,

You can add this PHP snippet using Code Snippet plugin; please check this doc How to add custom code snippets - HivePress Help Center

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.