I have a piece of code that extracts the first paragraph from the post, enclosed in a p tag.

I want to add a "Read more" link inside the p tag referring to "the full post".

Please help.

function first_paragraph() {
  global $post, $posts;
  $first_para = '';
  $post_content = $post->post_content;
  $post_content = apply_filters('the_content', $post_content);
  $output = preg_match_all('%(<p[^>]*>.*?</p>)%i', $post_content, $matches);
  $first_para = $matches [1] [0];
  echo $first_para;

1 Answer 1


One approach is:


// this is the resulted paragraph without the enclosing <p> and </p> 
$first_para_inner_text = $matches [1] [1]; // <-- the index changed

$link = get_permalink($post);

// rebuilding the p 
$first_para = '<p>' . $first_para_inner_text .  ' <a href="'.$link.'">Read More</a></p>';

echo $first_para;

Not tested, but you may get the idea. Just need to modify/change some parts of your code with this.

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.