get_the_excerpt() will get you just a teaser of your content. you can specify a post id if it’s outside a loop.
To get the blurred out version like the newspapers have on there site, we want our non-logged-in users to see a content teaser, the_excerpt()
AND we don’t want them to see the full content, just a blurry version.
We can use wp_get_current_user()->roles
to define an array of users that should be able to see the full post, and display a blurry version to the rest of them.
We can use the text-shadow
css propriety to blur-out our content. And just in case, we want to limit the number of characters being displayed, so we will cut out our blurred content too using substr
.
<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post();
the_title( '<h1>', '</h1>' );
the_excerpt();
$roles = array( 'administrator', 'editor', 'subscriber' );
if( array_intersect( $roles, wp_get_current_user()->roles ) ) {
the_content();
} else {
echo '<span style="text-shadow:0 0 8px white;color:transparent;">' . substr( wp_strip_all_tags( get_the_content(), true ), 0, 650 ) . '</span>';
};
endwhile;
endif; ?>
Function | Description |
---|---|
the_title |
Display or retrieve the current post title with optional markup. |
the_excerpt |
Display the post excerpt. |
wp_get_current_user |
Retrieve the current user object. |
the_content |
Display the post content. |
Here is our result: