- This topic has 1 reply, 2 voices, and was last updated 10 years, 4 months ago by Sakin.
-
AuthorPosts
-
May 19, 2014 at 7:47 am #31274AKMARK5000Member
My site is set up my site so that Users are able to submit (upload) their own
articles (posts). These posts can include linked pictures.Then I get an email notifying me of the pending post and I review and publish it. Then the post appears on the front-end slider as a featured post.
The problem I have is that when a User submits their post and it includes a linked pictures. The image to is not automatically the “Featured Image.”
I’ve not been able to find a code that will do what I want with Linked Images. The below code I found is cut/paste into the functions.php file. I prefer a plugin, but code works too. This code works if the images are already in my “Media Library” and then inserted into the post.
=======================================================================================
function auto_set_featured() {
global $post;
$has_thumb = has_post_thumbnail($post->ID);
if (!$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’, ‘auto_set_featured’);
add_action(‘save_post’, ‘auto_set_featured’);
add_action(‘draft_to_publish’, ‘auto_set_featured’);
add_action(‘new_to_publish’, ‘auto_set_featured’);
add_action(‘pending_to_publish’, ‘auto_set_featured’);
add_action(‘future_to_publish’, ‘auto_set_featured’);
=======================================================================================May 19, 2014 at 2:50 pm #31332SakinKeymaster@AKMARK5000: The code looks nice and you can add it in your child theme. No need to add any plugin for that. You should not edit any core theme files like functions.php, index.php, style.css and so in. As these files will be reverted back to original when you update the theme. That is why you need to build child theme and then create blank functions.php file and additional code only.
Please check this post on How to create WordPress Child Theme
-
AuthorPosts
- The topic ‘Linked Images as Featured Images’ is closed to new replies.