Site logo

Archived topic

Protected posts and excerpts

5 replies · Started by Peter on June 21, 2022

Viewing posts 1–6 of 6

Hi
Is there a way to show excerpts of posts whilst they're password protected?

I've tried the below code which works for the protected posts but wipes out all other posts which isn't helpful.

Many thanks in advance

Pete

function wpb_protected_excerpt( $excerpt ) {
if ( post_password_required() ) {
$post = get_post();
$excerpt=$post->post_excerpt;
}
return $excerpt;
}
add_filter( 'the_excerpt', 'wpb_protected_excerpt' );
 
function wpb_protected_excerpt_posts( $content ) {
if ( post_password_required() && is_single() ) {
$post = get_post();
 
return $post->post_excerpt.$content;
}}
add_filter( 'the_content', 'wpb_protected_excerpt_posts', 10 );

Hi there,

so which part breaks ? Is it on the single post where the content would normally be displayed ?

Hi David

Many thanks for getting back to me.
On the front page I have a section of protected posts and general news posts.
When I apply the above code the protected posts reveal the excerpt but hide all other posts on the page.
They're in different Show Post groups if that helps.
I just need to show the excerpt in the protected posts as well as all other posts.

Cheers

Pete

Hi Peter,

Try using just this code:

function wpb_protected_excerpt( $excerpt ) {
if ( post_password_required() ) {
$post = get_post();
$excerpt=$post->post_excerpt;
}
return $excerpt;
}
add_filter( 'the_excerpt', 'wpb_protected_excerpt' );

Then, add a custom Excerpt in the Excerpt field of the posts with protected passwords.

Kindly let us know how it goes.

Thanks Fernando that works great.
Much appreciated :)

You’re welcome Peter!

This archived topic is closed to new replies.