Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Need to display newest image attachment as featured image WordPress

  • SOLVED

I would like the latest image uploaded and attached to a post to be the NEW thumbnail for the post. Currently it defaults to the first attachment ever used for the post. Here is what I have for code.
function cc_thumbnail() {
global $post;
if (get_option('cc_default_thumbs') == 'yes') {
$defthumb = get_bloginfo('template_directory') . '/images/def-thumb.jpg';
} else {
$defthumb = false;
}
$cc_img = get_the_image(array(
'meta_key' => 'thumbnail',
'size' => 'thumbnail',
'default_image' => $defthumb,
'format' => 'array',
'image_scan' => true,
'link_to_post' => false
));
if ( $cc_img['url'] && get_option('cc_show_thumbs') == 'yes' && get_post_meta( $post->ID, 'remove_thumb', true ) != 'Yes' ) { ?>
<img class="thumbnail" src="<?php echo $cc_img['url']; ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" />
<?php }
}


and I'm using the following to display the thumbnail.

<?php cc_thumbnail(); ?>

Is it possible to modify this to tell the above code that the LAST attachment will be the NEW thumbnail instead of the first attachment to the post? Or what can I do to achieve that?

Thanks

Answers (1)

2020-05-18

Arief Fajar Nursyamsu answers:


function cc_thumbnail() {
global $post;
if (get_option('cc_default_thumbs') == 'yes') {
$defthumb = get_bloginfo('template_directory') . '/images/def-thumb.jpg';
} else {
$defthumb = false;
}
$size = 'thumbnail'; //**** use thumbnail instead post-thumbnail

$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_parent' => $post->ID
);
$images = get_posts($args);
foreach($images as $image) {
$att_ids[] = $image->ID;
}

$attachment_id = max($att_ids); // get latest attachment id
$cc_img = wp_get_attachment_image_src($attachment_id, $size);
$cc_img['url'] = $cc_img[0];
if ( $cc_img['url'] && get_option('cc_show_thumbs') == 'yes' && get_post_meta( $post->ID, 'remove_thumb', true ) != 'Yes' ) { ?>
<img class="thumbnail" src="<?php echo $cc_img['url']; ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" />
<?php
}
}


Try this..


twdesigns comments:

It did what I needed but all of my other posts that had the default thumbnail (if not featured or attached image) doesn't display an image now... ideas?


Arief Fajar Nursyamsu comments:

Modifying the code..


function cc_thumbnail() {
global $post;
if (get_option('cc_default_thumbs') == 'yes') {
$defthumb = get_bloginfo('template_directory') . '/images/def-thumb.jpg';
} else {
$defthumb = false;
}
$size = 'thumbnail'; //**** use thumbnail instead post-thumbnail

$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_parent' => $post->ID
);
$images = get_posts($args);
foreach($images as $image) {
$att_ids[] = $image->ID;
}
if(is_array($att_ids)){
$attachment_id = max($att_ids); // get latest attachment id
}
$cc_img = wp_get_attachment_image_src($attachment_id, $size);
$cc_img['url'] = $cc_img[0];
if(empty($cc_img['url'])){$cc_img['url']=$defthumb;}

if ( $cc_img['url'] && get_option('cc_show_thumbs') == 'yes' && get_post_meta( $post->ID, 'remove_thumb', true ) != 'Yes' ) { ?>
<img class="thumbnail" src="<?php echo $cc_img['url']; ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" />
<?php
}
}


twdesigns comments:

Works Awesome!