• Resolved newshop

    (@newshop)


    Hello, your plugin is one of the most helpful plugins overall! Thank you!
    I am wondering if its possible to set my custom post type like so:
    Use the url of the featured image as post url. So instead of linking to the post itself, it should link to the attachment page of the featured image.

    Is this possible?
    Any help would be really appreciated.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    If you mean the accompanying permalink for a given attachment, since attachments are posts in a post type themselves, then I think the following snippet would work for you. It’s not something available as a setting for CPTUI but I think this covers your need.

    
    function cptui_featured_img_permalink_as_post_type_permalink( $post_link, $post ) {
    	// Only published posts
    	if ( 'publish' !== $post->post_status ) {
    		return $post_link;
    	}
    
    	// CHANGE THIS TO MATCH
    	if ( 'movie' !== $post->post_type ) {
    		return $post_link;
    	}
    
    	// Only if we have a post thumbnail
    	if ( has_post_thumbnail() ) {
    		// We'll grab the thumbnail ID on the current post, and then grab that thumbnail's permalink and return that.
    		return get_permalink( get_post_thumbnail_id( get_the_ID() ) );
    	}
    
    	// Otherwise return the original value.
    	return $post_link;
    }
    add_filter( 'post_type_link', 'cptui_featured_img_permalink_as_post_type_permalink', 10, 2 );
    

    You’ll want to change the post type in the snippet to match yours. I used “movie” as a quick demo.

    Thread Starter newshop

    (@newshop)

    Thank you very much for your fast reply!
    Your code works, but I think my question was a bit confusing: I did not mean the attachment page but the image url itsef. So instead of linking to “mywebsite.com/?attachment_id=10356” it need to have a link like “mywebsite.com/redshirt.jpg” while redshirt.jpg is the featured image. Is this possible?
    Thank you in advance,
    kind regards

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Try this version

    
    function cptui_featured_img_permalink_as_post_type_permalink( $post_link, $post ) {
    	// Only published posts
    	if ( 'publish' !== $post->post_status ) {
    		return $post_link;
    	}
    
    	// CHANGE THIS TO MATCH
    	if ( 'movie' !== $post->post_type ) {
    		return $post_link;
    	}
    
    	// Only if we have a post thumbnail
    	if ( has_post_thumbnail() ) {
    		// We'll grab the thumbnail ID on the current post, and then grab that thumbnail's image src at full size and return that.
    		return wp_get_attachment_image_url( get_post_thumbnail_id( get_the_ID() ), 'full' );
    	}
    
    	// Otherwise return the original value.
    	return $post_link;
    }
    add_filter( 'post_type_link', 'cptui_featured_img_permalink_as_post_type_permalink', 10, 2 );
    
    Thread Starter newshop

    (@newshop)

    You’re genius!!! Works like a charm! Thank you so much!!

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Welcome

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Use featured image url as post url’ is closed to new replies.