• Sabinooo

    (@sabinooo)


    Hello!

    I thought this could interest more people, I am humbly offering you here a minor alteration I have brought to the Hueman theme, so that:
    – when we have a post for which no Featured image has been defined
    – the theme will simply take the first image embedded in the post (with img src), and make it the officially Featured image of that post

    Pros: excellent for old blogs adopting the Hueman theme, whose posts were written without caring at all about attributing a special status (such as: Featured) to a special image.
    Those old blog posts look very ugly with Hueman’s default “no image” display.
    Better take their first image and make it the featured image. Now, they’re looking fresh and dandy!
    Cons: one additional SQL query for every post to display in the page. Shouldn’t be much of an issue, hopefully.
    I suspect there might be an additional faint load for the image processing, but I cannot tell if it would have a significant impact.
    Lacking feature: to save to the post, as featured, the first chosen image, it would only have server impact once, on the first display. Beats me, no idea how it could be done, even if it would be awesome (I searched and tested, no plugin does it yet for already existing posts).

    I claim no credit, this code was offered to me by a very kind soul over the internet, called Noah, I simply tested that it works!
    That’s why I’m sharing it here, with hopes to help, perhaps, other people. And – shamefully-, also with the hope to see it integrated into the official theme: this way I wouldn’t need to branch off to a child theme and give up on future Hueman updates ??

    The code, now:
    in functions.php

    /*  Noah Custom - Get first image of post
    /* ------------------------------------ */
    function get_first_image() {
      global $post, $posts;
      $first_img = '';
      ob_start();
      ob_end_clean();
      $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
      $first_img = $matches[1][0];
    
      if(empty($first_img)) {
        $first_img = "empty";
      }
      return $first_img;
    }

    in content.php

    <?php elseif ( get_first_image() != 'empty' ): ?>
            	<img src="<?php echo get_first_image(); ?>" alt="<?php the_title(); ?>" />

    in content-featured.php

    <?php elseif ( get_first_image() != 'empty' ): ?>
            	<img src="<?php echo get_first_image(); ?>" alt="<?php the_title(); ?>" />

    in content-standard.php

    <?php elseif ( get_first_image() != 'empty' ): ?>
            	<img src="<?php echo get_first_image(); ?>" alt="<?php the_title(); ?>" />

    That’s it!
    I just remembered to have to add it to the “you might also like” miniatures, but it’s nothing crazy to add, I just haven’t tested that addition yet.

    I hope this change suggestion might be of help to some people in the future ??
    (And, who knows, maybe that could go to core, I can dream!)

Viewing 3 replies - 1 through 3 (of 3 total)
  • bdbrown

    (@bdbrown)

    Hi Sabinooo. Thanks for the contribution. Looks like a pretty straight-forward implementation. Perhaps Alex will consider it for a future update.

    Didu

    (@dashingdidu)

    Not working on my site My Web Cafe

    Thread Starter Sabinooo

    (@sabinooo)

    Can’t help with that, I gave up on Hueman a long time ago.

    I dislike having to rely on child themes because they “fixate” the theme on php files that become obsolete as new updates on the “real” theme accumulate over time. There could be important security holes patched for all we know, and yet our child theme would still tell wordpress to use the old exploitable php files in which we have made changes.
    I know there is no ideal solution, we can’t tell wordpress “look here boy, paste this php code between THIS and THAT, thanks”, it wouldn’t work. But forcing the usage of php files that are obsolete should come with the habit of reading changelogs and propagating important changes, something only very few people will do.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Humble code proposition: display first image as Featured when none is defined’ is closed to new replies.