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

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' );
  • 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 Feb 25 '15 at 20:38
  • Didn't understood really well what you were trying to say, can you elaborate a bit more? – Ramy Deeb Feb 26 '15 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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