Site logo

Archived topic

Display another image instead of featured image on condition

3 replies · Started by Andrew on February 16, 2020

Viewing posts 1–4 of 4

Hi,

I'm using Woocommerce's Membership plugin to display content for my members only. This means some posts are public and some are accessible for members only. With this plugin, when you specify that a post is for members only, it replaces the_content() with whatever message you want to display.

My issue is that I also need to replace the featured image with another image for non-members. I really don't know how to do this, but I went in this direction:

add_filter( 'generate_featured_image_output', 'edf_display_featured_image' );
function edf_display_featured_image(){
	if(current_user_can('wc_memberships_view_restricted_post_content',get_the_ID() ) ){
		// Display post image
	} else {
		// Display image ID 1699
}

Any way you could help me out with the // Display post image and // Display image ID 1699 parts?

Thanks a lot for your help

Andrew

Hi there,

You could try this:

add_filter( 'get_post_metadata', function( $output, $object_id, $meta_key ) {
    if ( '_thumbnail_id' === $meta_key ) {
        if ( ! current_user_can( 'wc_memberships_view_restricted_post_content', get_the_ID() ) ) {
            return '1699';
        }
    }

    return $output;
}, 10, 3 );

Let me know :)

Hi Tom, works like a charm! Thanks so much

You're welcome :)

This archived topic is closed to new replies.