Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
[WP][PHP] Split first paragraph from main content, display it elsewhere
// 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; ?>
// Second paragraph onwards
$allp = wpautop ( $post->post_content );
$allp = substr( $allp, strlen($p1));
<div class="rest_of_content">
<?php echo $allp; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.