[Resolved] Link image in custom filed to the post url

Please login to receive premium support.

Support for the free plugin can be found here.

Home Forums Pro Support Link image in custom filed to the post url

This topic contains 3 replies, has 2 voices, and was last updated by  Tom 7 months ago.

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

    Daniel
    Participant

    Hi!

    I used this piece of code:

    add_action( 'wpsp_before_title', 'tu_add_custom_meta' );
    function tu_add_custom_meta() {
        $image = get_post_meta( get_the_ID(), 'cami_thumb', true );
    
        if ( isset( $image ) && '' !== $image ) {
            echo '<div class="imagen-producto"><img src="' . $image . '"></div>';
        }
    
    }

    to display an image stored in a custom field.

    Now I would like to link it to the url of the post, how can I do it??

    #6479

    Tom
    Keymaster

    Try this:

    add_action( 'wpsp_before_title', 'tu_add_custom_meta' );
    function tu_add_custom_meta() {
        $image = get_post_meta( get_the_ID(), 'cami_thumb', true );
        $permalink = get_permalink();
    
        if ( isset( $image ) && '' !== $image ) {
            echo '<div class="imagen-producto"><a href="' . $permalink . '"><img src="' . $image . '"></a></div>';
        }
    }
    #6488

    Daniel
    Participant

    Thanks Tom!

    Of course it works perfectly.

    #6495

    Tom
    Keymaster

    You’re welcome 🙂

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.