How to programmatically set existing image in media library the featured image for a post in wordpress
How to programmatically set existing image in media library the featured image for a post in wordpress
How to programmatically set existing image in media library the featured image for a post in wordpress
Have you already used this image in a different post? The post_parent may already be assigned to a different post ID from the one you are on. The code you have will only return images uploaded to this post first. I’ve seen a few regex type solutions to this problem, however I think PHPs DOMDocument … Read more
Featured image size always cropped bottom
I’m guessing the problem is this: $recent_posts[0][‘ID’]; and I don’t see $recent_posts being defined in your code, so what exactly is that $recent_posts and where/how did you define it?: $image = wp_get_attachment_image_src( get_post_thumbnail_id($recent_posts[0][‘ID’]), ‘full’ ); And you’re inside The Loop, so you could just call get_post_thumbnail_id() without having to specify the post ID — or … Read more
Thumbnails regenerate wp_generate_attachment_metadata
In single.php you can wrap your featured image in a link tag with a conditional to check if the user is logged in. if ( is_user_logged_in() ) { echo ‘<a href=”https://google.com/”>’; } the_post_thumbnail(); if ( is_user_logged_in() ) { echo ‘</a>’; }
What you’re apparently asking for isn’t possible. WordPress will never add white space to an image to make it fit a certain size. This is what cropping means. Cropping will always remove (i.e. crop) some of the image so that it fits a certain size. As far as I am aware, there is no tool … Read more
I’ve reached the “problem” My theme had no “thumbnail” suport. I just add the line add_theme_support( ‘post-thumbnails’ ); on funcions.php Veja detalhes em https://codex.wordpress.org/Post_Thumbnails
You can test if the $thumb has any contents and then apply a class to the featured image block that you can use to hide the container. <?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), ‘full’ ); ?> <div id=”post” class=”featured-image <?php echo empty($thumb) ? ‘no-featured-image-class’ : ”; ?>” style=”background-image: url(‘<?php echo $thumb[‘0′]; ?>’)”>
Please try the code given below: function auto_featured_image() { global $post; if (!has_post_thumbnail($post->ID)) { $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); } } } } // Use it temporary to generate all featured images add_action(‘the_post’, ‘auto_featured_image’); // Used for new posts add_action(‘save_post’, ‘auto_featured_image’); add_action(‘draft_to_publish’, ‘auto_featured_image’); … Read more