<?php
// Add Shortcode
function blog_shortcode( $atts ) {
// Attributes
extract( shortcode_atts(
array(
'id' => '',
), $atts )
);
// Code
if (has_post_thumbnail()) {
$url = wp_get_attachment_url( get_post_thumbnail_id( $id ) );
}
if ( isset( $id ) ) {
return '<img src="' . $url . '"/><a href="' . get_permalink( $id ) . '">' . get_the_title( $id ) . '</a>';
}
}
add_shortcode( 'blog', 'blog_shortcode' );
?>
I’m trying to make a shortcode to where I can basically type [blog id=”xxxxxx”] and I’ll get a link and thumbnail. Right now I’m getting the link but the thumbnail isn’t working. I don’t think I fully understand how to get the thumbnail ID to become a url for the < img > tag. Any help will be appreciated.
You are assigning the attachment URL to your
$url
variable:then passing that same variable back to
wp_get_attachment_url
:You should simply output it instead:
EDIT:
You are missing the post ID from has_post_thumbnail. The following should work.