Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Jany-M
Last active November 26, 2019 12:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Jany-M/5f581246fe0c5c1bed7b37085aef7e60 to your computer and use it in GitHub Desktop.
Save Jany-M/5f581246fe0c5c1bed7b37085aef7e60 to your computer and use it in GitHub Desktop.
[WP][PHP] Split first paragraph from main content, display it elsewhere
<?php
// Grab the first paragraph, show it where you need it, then take the rest of the content and remove the first paragraph and show it elsewhere
// The script uses WordPress functions/content but can be used in any PHP script, just replace the WP functions
// First Paragraph
global $post;
$p1 = wpautop( $post->post_content );
$p1 = substr( $p1, 0, strpos( $p1, '</p>' ) + 4 );
//$p1 = strip_tags($p1, '<a><strong><em><h3><h2><i>'); // in case you need to remove some tags, add the ones you want to KEEP here
?>
<div class="first_paragraph">
<?php echo $p1; ?>
</div>
<?php
// Second paragraph onwards
$allp = wpautop ( $post->post_content );
$allp = substr( $allp, strlen($p1));
?>
<div class="rest_of_content">
<?php echo $allp; ?>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment