How to put "Read more" link in Custom Excerpt inside p tag?

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 = '';
  ob_start();
  ob_end_clean();
  $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;
} 

Topic functions read-more excerpt php Wordpress

Category Web


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.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.