Dynamic Featured Image - WordPress - is not getting the first featured image
I've installed the latest Dynamic Featured Image 3.1.2 and when I try to just print out the array of featured images on the page I only get Featured Image 2 and onward.
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php if( class_exists('Dynamic_Featured_Image') ) {
global $dynamic_featured_image;
$featured_images = $dynamic_featured_image->get_featured_images( );
print_r( $featured_images );
//You can now loop through the image to display them as required
} ?>
... << rest of Post Loop >>
Even though I've added more than one Featured image it only shows from Featured Image 2 and on in the array.
Did I miss something else that I need to do?
Solutions
You can get all featured images including the default WordPress one using this function:
/**
* Retrieve featured images for specific post(s)
* including the default Featured Image
*
* @since 3.1.2
* @access public
*
* @see $this->get_featured_images()
*
* @param Integer $post_id id of the current post
*
* @return Array An array of images or an empty array on failure
*/
public function get_all_featured_images( $post_id = null ) {
if ( is_null( $post_id ) ) {
global $post;
$post_id = $post->ID;
}
$thumbnail_id = get_post_thumbnail_id( $post_id );
$featured_image_array = array();
if ( ! empty( $thumbnail_id ) ) {
$featured_image = array(
'thumb' => wp_get_attachment_thumb_url( $thumbnail_id ),
'full' => wp_get_attachment_url( $thumbnail_id ),
'attachment_id' => $thumbnail_id
);
$featured_image_array[] = $featured_image;
}
$dfiImages = $this->get_featured_images( $post_id );
$all_featured_images = array_merge( $featured_image_array, $dfiImages );
return $all_featured_images;
}
This function will be included in plugin in the next release. Till then you can add it to your
dynamic-featured-image.php
plugin file or you can download the and use the file from the master branch of github.
P.S. I am the author of the plugin.
What you are printing in your code are the custom Featured Images ( Featured Image 2 onwards) added by the plugin Dynamic Featured Image 3.1.2:
<?php if( class_exists('Dynamic_Featured_Image') ) {...
..while the first "
Featured Image
" in your Edit post screen is implemented in the wordpress core itself and can be printed using the
the_post_thumbnail
function (see http://codex.wordpress.org/Function_Reference/the_post_thumbnail).