Last active
December 23, 2015 01:59
-
-
Save theukedge/6563430 to your computer and use it in GitHub Desktop.
Show first n paragraphs - WordPress
This file contains 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 | |
// FILTER POST CONTENT - SHOWS FIRST N PARAGRAPHS | |
function filter_content_n_paragraphs( $content ) { | |
$stopat = 2; // how many paragraphs you want to display (n) | |
$paragraphno = $stopat - 1; // computers start at 0 | |
// conditions to meet for filtering content | |
if( !is_user_logged_in() ) { | |
$paras = explode( '</p>', $content ); | |
// check whether article is at least n paragraphs long | |
if( count( $paras ) > $paragraphno ) { | |
$i = 0; | |
// show paragraph while less than our paragraph no | |
while( $i <= $paragraphno ) { | |
echo $paras[$i]; | |
$i++; | |
} | |
} | |
// how the user can see all the content | |
return gravity_form(2, false, true, false); | |
} else { | |
return $content; | |
} | |
} | |
add_filter( 'the_content', 'filter_content_n_paragraphs' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment