0

So I'm trying to create a shortcode to get specific id and its featured image to display it in a specific page. I want to use shortcode to make it easier for the user to add any post in this page.

I've tried a simple code which works but only display title, I'm not sure how I can get the featured image instead of the title.

function post_shortcode( $atts ) {
    extract(
        shortcode_atts(
            array(
                'id' => '',
            ),
            $atts
        )
    );

    if ( isset( $id ) ) {
        return '<a href="' . get_permalink( $id ) . '">' . get_the_title( $id ) . '</a>';
    }
}

add_shortcode( 'test', 'post_shortcode' );
1

1 Answer 1

1

Using your example I've updated it to return instead of the title the image.

function post_shortcode( $atts ) {
    extract(
        shortcode_atts(
            array(
                'id' => '',
            ),
            $atts
        )
    );

    if ( isset( $id ) ) {
        return '<a href="' . get_permalink( $id ) . '">' . get_the_post_thumbnail( $id, 'thumbnail' ) . '</a>';
    }
}

add_shortcode( 'test', 'post_shortcode' );
2
  • Cool thanks, I want to remove a href as I want each thumbnail to go to different external links. But when I remove it I get unidentified thumbnail.
    – MrNew
    Commented Feb 25, 2015 at 20:38
  • Didn't understood really well what you were trying to say, can you elaborate a bit more?
    – Ramy Deeb
    Commented Feb 26, 2015 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.