Skip to content

Instantly share code, notes, and snippets.

@joecue
Forked from Jany-M/manage_paragraphs.php
Created November 14, 2019 16:14
Show Gist options
  • Save joecue/12291e3ecba60b7d1d09943174004680 to your computer and use it in GitHub Desktop.
Save joecue/12291e3ecba60b7d1d09943174004680 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