[Resolved] Display another image instead of featured image on condition

Home Forums Support [Resolved] Display another image instead of featured image on condition

Home Forums Support Display another image instead of featured image on condition

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1167383
    Andrew

    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

    #1167581
    Tom
    Lead Developer
    Lead Developer

    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 ๐Ÿ™‚

    #1167651
    Andrew

    Hi Tom, works like a charm! Thanks so much

    #1168213
    Tom
    Lead Developer
    Lead Developer

    You’re welcome ๐Ÿ™‚

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.