Below, you’ll find a detailed guide on how to add the Featured Image Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Featured Image Plugin shortcodes not to show or not to work correctly.
Before starting, here is an overview of the Featured Image Plugin and the shortcodes it provides:
"Featured Image is a dynamic WordPress plugin designed to enhance your posts' visual appeal. It allows easy setting and management of post thumbnails, enhancing your site's aesthetic and user engagement."
- [featured-img]
- [featured-img-caption]
Featured Image [featured-img] Shortcode
The ‘featured-img’ shortcode is used to retrieve and display the featured image of a post. It utilizes the ‘wp_get_attachment_image_src’ function to get the URL of the image. If an alt text is set, it will also be included in the output. If no featured image is found, the function will return null. This shortcode makes it easy to include a post’s featured image anywhere on your site.
Shortcode: [featured-img]
Examples and Usage
Basic Example – A simple shortcode usage to fetch the featured image of the current post.
[featured-img /]
Advanced examples
For the advanced usage of the shortcode, we would need to modify the PHP function to accept parameters or attributes. However, given the current function, there are no parameters being accepted. Therefore, we can’t provide an advanced example with more than 2 parameters. But we can provide an example where we use the shortcode within a post loop to display the featured images of all posts.
<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post(); ?>
[featured-img /]
<?php endwhile;
endif;
?>
Please note, this will only work in the WordPress loop and the shortcode will fetch the featured image of the respective post in the loop.
PHP Function Code
In case you have difficulties debugging what causing issues with [featured-img]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode('featured-img', 'getting_featured_img');
Shortcode PHP function:
function getting_featured_img() {
global $post;
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
$alt = get_post_meta(get_post_thumbnail_id( $post->ID ), '_wp_attachment_image_alt', true);
if($image)
{
if ($alt)
{
$mpfeatureimg = " <div id='featured-img-id'><img src='" ;
$mpfeatureimg .= $image[0];
$mpfeatureimg .= "' alt='";
$mpfeatureimg .= $alt;
$mpfeatureimg .= "' /></div>";
}
else
{
$mpfeatureimg = " <div id='featured-img-id'><img src='" ;
$mpfeatureimg .= $image[0];
$mpfeatureimg .= "' /></div>";
}
}
else
{
$mpfeatureimg = null;
}
return $mpfeatureimg;
}
Code file location:
featured-image/featured-image/featured-image.php
Featured Image [featured-img-caption] Shortcode
The ‘featured-img-caption’ shortcode is a WordPress function that retrieves the caption of a featured image. This shortcode uses the ‘get_post_thumbnail_id’ and ‘get_posts’ functions to achieve this. The PHP function ‘getting_featured_img_caption’ fetches the ID of the featured image, then uses this ID to get the image’s caption. The caption is then returned and can be displayed on the website.
Shortcode: [featured-img-caption]
Examples and Usage
Basic example – The shortcode ‘featured-img-caption’ fetches the caption of the featured image of a post.
[featured-img-caption /]
Advanced examples
Using the shortcode to display the caption of the featured image by referencing the post ID. This will allow you to fetch the featured image caption of a specific post.
[featured-img-caption id=15 /]
Using the shortcode to display the caption of the featured image by referencing the post type. This will allow you to fetch the featured image caption of a specific post type.
[featured-img-caption post_type='page' /]
Please note that in the given PHP code, there are no parameters/attributes being used. Hence, the shortcode does not accept any parameters in its current form. To use parameters such as ‘id’ and ‘post_type’ in the shortcode, the PHP function ‘getting_featured_img_caption’ needs to be modified accordingly.
PHP Function Code
In case you have difficulties debugging what causing issues with [featured-img-caption]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode('featured-img-caption', 'getting_featured_img_caption');
Shortcode PHP function:
function getting_featured_img_caption() {
$thumbnail_id = get_post_thumbnail_id($post->ID);
$thumbnail_image = get_posts(array('p' => $thumbnail_id, 'post_type' => 'attachment'));
return $thumbnail_image[0]->post_excerpt;
}
Code file location:
featured-image/featured-image/featured-image.php
Conclusion
Now that you’ve learned how to embed the Featured Image Plugin shortcodes, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.
Leave a Reply