Tagged: x
-
AuthorPosts
-
September 27, 2016 at 11:44 am #1192564
Hi,
Is there a way to automatically set the featured image in my pages and posts, for example by taking the “medium” size of the first image in the page or post.
I tried this code but it doesn’t seem to work:
function autoset_featured() { global $post; $already_has_thumb = has_post_thumbnail($post->ID); if (!$already_has_thumb) { $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" ); if ($attached_image) { foreach ($attached_image as $attachment_id => $attachment) { set_post_thumbnail($post->ID, $attachment_id); } } } } add_action('the_post', 'autoset_featured'); add_action('save_post', 'autoset_featured'); add_action('draft_to_publish', 'autoset_featured'); add_action('new_to_publish', 'autoset_featured'); add_action('pending_to_publish', 'autoset_featured'); add_action('future_to_publish', 'autoset_featured');
many thanks
September 27, 2016 at 12:08 pm #1192590Hi There,
Please try with this code:
add_action( 'save_post', 'fb_set_featured_image' ); function fb_set_featured_image() { if ( ! isset( $GLOBALS['post']->ID ) ) return NULL; if ( has_post_thumbnail( get_the_ID() ) ) return NULL; $args = array( 'numberposts' => 1, 'order' => 'ASC', // DESC for the last image 'post_mime_type' => 'image', 'post_parent' => get_the_ID(), 'post_status' => NULL, 'post_type' => 'attachment' ); $attached_image = get_children( $args ); if ( $attached_image ) { foreach ( $attached_image as $attachment_id => $attachment ){ set_post_thumbnail( get_the_ID(), $attachment_id ); } } }
You have to upload at least one image to your posts / pages.
Hope it helps 🙂
September 27, 2016 at 12:22 pm #1192618Hi,
I just tried this on a page it doesn’t seem to work. (I press the “Update” button).
All of pages are created with cornerstone and all images are added with cornerstone if that helps!
September 27, 2016 at 1:08 pm #1192694Hi there,
This kind of customization is out of our support scope but we can have a look. I can’t guarantee but we want to see if that works. Would you mind providing us with login credentials so we can take a closer look? To do this, you can make a post with the following info:
– Link to your site
– WordPress Admin username / password
– FTP credentialsDon’t forget to select Set as private reply. This ensures your information is only visible to our staff.
September 27, 2016 at 2:24 pm #1192778This reply has been marked as private.September 28, 2016 at 12:21 am #1193421Hi,
You can try this third party plugin instead.
https://wordpress.org/plugins/set-all-first-images-as-featured/
Hope that helps.
September 29, 2016 at 3:20 pm #1196286Unfortunately this is and outdated plugin and doesn’t seem to work. This is what I was told:
WP does not differentiate what size image is being set as the post thumbnail/featured image. All images sizes for any given image all have the same ID value, each image size does not get its own ID. Because of this it is not possible to insert a specific sized image as the featured image.
What size image is shown on the site is dependent on how the image is added to the theme. There are several functions that can be used to add an image to a page, for example: https://developer.wordpress.org/reference/functions/the_post_thumbnail/
the_post_thumbnail($size);
If size is not given in this function then it is assumed you want to show the “post-thumbnail” size, whatever that is.
Your theme code can be adding the image in any number of ways, but it is there that you need to adjust the image size that is displayed and not when adding an image as the featured image for a post.
Could you tell me where the theme is doing that and I will edit that file?
September 29, 2016 at 8:54 pm #1196753Hi there,
Featured images are only applicable for the post by default. You have to add thumbnail support for page type too.
https://codex.wordpress.org/Function_Reference/add_post_type_support
Example, please add this code to your child theme’s functions.php
function enable_page_thumbnail() { add_post_type_support( 'page', 'thumbnail' ); } add_action( 'init', 'enable_page_thumbnail' );
Then you can now add featured image to your pages. Images are generated based on registered sizes, it’s nothing to do with setting the featured image. But you may need to regenerate your thumbnails using Force Regenerate Thumbnails plugin to make sure the sizes are properly pickup when the_post_thumbnail() is called.
Cheers!
-
AuthorPosts