Last active
November 26, 2019 12:10
-
-
Save Jany-M/5f581246fe0c5c1bed7b37085aef7e60 to your computer and use it in GitHub Desktop.
[WP][PHP] Split first paragraph from main content, display it elsewhere
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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