0

It's probably simple. I'm looking for a simple shortcode that accepts a single post id variable, and pulls in title / permalink / excerpt and post thumbnail.

I know there are plugins out there that will handle this but they all seem like so much overkill.

Would love some help on this thanks!

2

All you need to do is to write a shortcode add wrap a function around it:

function get_post_details($atts) {
    // Get the shortcode's attributes
    $atts = shortcode_atts( array(
        'post' => 0,
    ), $atts, 'get-post-details' );
    // Fetch the ID from $atts array
    $id = $atts['post'];
    // Stop the shortcode if no ID is set
    if ($id == '0') return 'Please provide a post ID.';
    // Get the title for this post
    $data = get_the_title($id);
    // Get the permalink for this post
    $data .= get_the_permalink($id);
    // Get the thumbnail for this post
    $data .= get_the_post_thumbnail_url($id, 'thumbnail');
    // Return the data
    return $data;
}
add_shortcode( 'get-post-details', 'get_post_details' );

Now you can have a post's details by using [get-post-details post="123"]. If no ID is set, the details of the current post in the loop will be shown.

2
  • Ah I think i see where our going with this - however your function doesn't appear to be working in my functions.php - It's sending the site into a 500 error and I can't see to sort what's throwing it off.
    – Tanmccuin
    Jul 3 '17 at 15:35
  • @Tanmccuin Actually I have no idea why it's throwing a 500 error, that's weird. But I've added more conditionals to make sure attributes are set, preventing some errors.
    – Jack Johansson
    Jul 3 '17 at 15:49

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.