Set featured images automatically for WordPress without the properties field

Adding featured images to articles in the main page may be cumbersome using the properties field for all articles, for example, if you have many articles, you will have to add a new field for each article, and this will require a great effort from you, but there are always solutions to these problems. With a little searching I found a simple solutionHappy new year on the occasion of the blessed month of Ramadan, may God bring it back to you with goodness, Yemen and blessings, and we wish you continued health and wellness – congratulations for the year 2011

This explanation has been updated because in the old templates it was necessary to set the prominent image from the properties field, but now things are easier than before. In general, I added a new hack to do this operation instead of the old code

Featured image designation requirements

You must make sure that the template you are using supports prominent images. There are two ways to use it:

  1. Using the Featured Images Feature in WordPress 2.9
  2. How to display featured image by TimThumb

Set highlights automatically

This method depends on the first image attached to the article, which is used as a prominent image for the article. In order for this method to work, you must add the code below to the template properties file functions.php

2* Get attachment id for default image url
3* $default url
5function get_attachment_id_from_src($image_src) {
6global $wpdb;
7$query = “SELECT ID FROM {$wpdb->posts} WHERE guid=’$image_src'”;
8$id = $wpdb->get_var($query);
9return $id;
12* Auto set featured image
13* Note : image must be in post attachemnts
15function autoset_featured() {
16global $post;
17$postsetid = get_the_ID();
18$already_has_thumb = has_post_thumbnail($post->ID);
19$default = ‘http://link-to-image’;
21if (!$already_has_thumb) {
22$attached_image = get_children(‘post_type=attachment&post_mime_type=image&post_parent=’.$postsetid );
23if ($attached_image) {
24foreach ($attached_image as $attachment_id => $attachment) {
25set_post_thumbnail($post->ID, $attachment_id);
27} else {
28set_post_thumbnail($post->ID, get_attachment_id_from_src(get_attachment_id_from_src($default)));
32add_action(‘the_post’, ‘autoset_featured’);

You must pay attention to something, the default image of the article must be inserted in the variable “default” on line 11 in order for it to be displayed in the event that no prominent image is available

the old way

Open the template’s functions.php file in order to add the hack inside it. If you do not have a properties file, create a new one and then paste the code below. This hack takes the link of the first image in your post and uses it as a thumbnailFailed loading gist error

Add thumbnail code

Here we will insert the code that will display thumbnails on the home page. Open the Index.php or home.php file, depending on the template files. The code must be inserted inside the_loop in order for it to display well without problems. It is recommended that you put it above the_content and this code

1<div class=”thumbnail”>
2<a href=”<?php the_permalink(); ?>” title=”<?php the_title_attribute(); ?>”>
3<img src=”<?php bloginfo(‘template_directory’); ?>/timthumb.php?src=<?php getImage(‘1’); ?>&w=150&h=150&zc=1″>

This code will create an image with a size of 150 x 150 pixels. If you want to change the size, you only need to change the ‘w’ and ‘h’ from the code. Please review the post  How to put image thumbnails and TimThumb script because you must create a cache folder inside the template folder and give it the explicit 777 or 755 Best if the pictures don’t show up

Add formatting to the style file

Here you must add a format to the style file to display the image in the place you want and the format you like, and this is a simple code for the display method

1.thumbnail a:link,
2.thumbnail a:visited {
9margin:5px 5px 0 0;
11.thumbnail a:hover,
12.thumbnail a:active {

end of transmission 😀

The advantages of this method are that you will not need to upload more images and will not use private fields. If there is any query please put a comment? And don’t forget to subscribe to our mailing list, thank you very much

Leave a Comment