Skip to content

Instantly share code, notes, and snippets.

@theukedge
Last active December 23, 2015 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theukedge/6563430 to your computer and use it in GitHub Desktop.
Save theukedge/6563430 to your computer and use it in GitHub Desktop.
Show first n paragraphs - WordPress
<?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