skip to Main Content

`

$metadescription = wp_trim_words( get_the_content(), 55, '');

add_post_meta($post_id, 'meta_description', '$metadescription', true);`

but output is $metadescription

anyone help me please

`$metadescription = wp_trim_words( get_the_content(), 55, ”);

add_post_meta($post_id, ‘meta_description’, ‘$metadescription’, true);“

3

Answers


  1. Try out the below code:

    For the variable, you don’t need to use '' for that.

    $metadescription = wp_trim_words( get_the_content(), 55, '');
    
    add_post_meta($post_id, 'meta_description', $metadescription , true);
    
    Login or Signup to reply.
  2. First, make sure that get_the_content is returning the content you want and $meta_description isn’t null.

    Try This:

    add_post_meta($post_id, 'meta_description', '' . $meta_description . '', true);
    
    Login or Signup to reply.
  3. I found the best option, please try this.

    global $post;
    $metadescription = wp_trim_words( apply_filters('the_content', $post->post_content), 55, '');
    add_post_meta($post_id, 'meta_description',$metadescription, true);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search