It wasn’t quite as easy as @Pat J made it sound, especially if you want HTML
formatting with your copy. I cribbed from this answer and came up with the following code for you. I’ve tested it and it works:
// If the user is logged in, display the full content
if(is_user_logged_in()):
the_content();
else: // The user isn't logged in and should only see the first 300 words
echo force_balance_tags( html_entity_decode( wp_trim_words( htmlentities( wpautop(get_the_content()) ), 300, '...' ) ) );
endif;
UPDATE
There are 2 errors in your code. One is the ;
after the initial if
statement, and the arrays
don’t match: $roles
v $role
.
The following code integrates the original answer (logged in/out) with your modifications (if is array):
if(is_user_logged_in()):
if( in_array( 'administrator', $roles ) || in_array( 'pmpro_role_2', $roles ) || in_array( 'pmpro_role_1', $roles )):
the_content();
else:
echo force_balance_tags( html_entity_decode( wp_trim_words( htmlentities( wpautop(get_the_content()) ), 300, '...' ) ) );
endif;
else:
echo force_balance_tags( html_entity_decode( wp_trim_words( htmlentities( wpautop(get_the_content()) ), 100, '...' ) ) );
endif;
Good luck!