Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #31274
    AKMARK5000
    Member

    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’);
    =======================================================================================

    #31332
    Sakin
    Keymaster

    @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

Viewing 2 posts - 1 through 2 (of 2 total)
  • The topic ‘Linked Images as Featured Images’ is closed to new replies.