0

I was trying to use the_content as my meta description, however, i noticed that the texts/content is way too many so I'm thinking if it's possible to use the first paragraph only?

Here's the code I'm currently using for meta description.

<?php global $post;
$content = $post->post_content; if(!empty($content)) {?>
<meta property="og:description" content="<?php  echo strip_tags($content); ?>" />
<?php }?>

Btw, The first paragraph of my the_content has a class. f-desc and here's the code I've used to add it.

function first_paragraph($content){
    return preg_replace('/<p([^>]+)?>/', '<p$1 class="f-desc">', $content, 1);
}
add_filter('the_content', 'first_paragraph');

1 Answer 1

1

You can use the_excerpt() instead of the_content

Reference link for filter your excerpt : show-first-paragraph Or you can also do this by callback function inside your loop using the function to modify the_content as below:

function get_first_paragraph(){
    global $post;

    $str = wpautop( get_the_content() );
    $str = substr( $str, 0, strpos( $str, '</p>' ) + 4 );
    $str = strip_tags($str, '<a><strong><em>');
    return $str;
}

Hope this helps!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.