Oct 9, 2012

That first image is automatically dial an entry as highlighted in WordPress

Filled under:

That first image is automatically dial an entry as highlighted in WordPress

Since the appearance of the images featured in WordPress is a widely used resource for all kinds of new-style themes, becoming almost essential.

However, if you already have a site with many entries, set the images to highlight in old entries can be a real pain in the ass , being benevolent.


Fortunately there are ways to do so we do not have to go manually adding each entry featured images do you see?.

First we see the ways that we have seen earlier in WordPress Help to define default featured images:


  1. Thumbnails by default on all inputs : by code or plugin can define a featured picture "to capon" in all entries, something great if you do not want to complicate too.
  2. Making several outstanding images at once : a finer way, where at least define outstanding images from the Media window, can add images in batches.
  3. Automate outstanding images by category : spinning even finer in this case, we define an image as highlighted by default according to the category of the entry. The code would look something like this:



< ?php if(in_category('categoria-1') { ?>
<img src="ruta-carpeta-imagen/categoria-1.jpg" alt="Categoria 1" />
<?php } elseif(in_category('categoria-2') { ?>
<img src="ruta-carpeta-imagen/categoria-2.jpg" alt="Categoria 2" />
<?php } else { ?>
<img src="ruta-carpeta-imagen/no-category.jpg" alt="Otra categoria" />
<?php } ?>


And, returning to the issue that brings us here today, a more elegant solution, tailored to each entry, WordPress would take the first picture you find in each entry and use as outstanding image right?.

Well, we can do this fancy stuff in two ways:

First picture ... as automatically highlighted code ...

What we have to do is open our plugin function and add this code:


//primera imagen como destacada
function auto_post_thumbnail() {
          global $post;
          $already_has_thumb = has_post_thumbnail($post->ID);
              if (!$already_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);
                                }
                           } else {
                                set_post_thumbnail($post->ID, 'ID_imagen_por_defecto');
                           }
                        }
      }
 //Ahora añadimos la función al loop
add_action('the_post', 'auto_post_thumbnail');
add_action('save_post', 'auto_post_thumbnail');
add_action('draft_to_publish', 'auto_post_thumbnail');
add_action('new_to_publish', 'auto_post_thumbnail');
add_action('pending_to_publish', 'auto_post_thumbnail');
add_action('future_to_publish', 'auto_post_thumbnail');


You just have to modify the code in line 12, substituting " ID_imagen_por_defecto "by the ID of the default image to show if there were any images on some input. To find the ID you can use the trick explained in the article on how to include and exclude images in WordPress galleries .

The rest of the code does what it promises : take the first image it finds in the input and as a leading brand.


First picture ... as outstanding plugin automatically ...

And if you get scared still fiddling code and enjoy the great virtues of totally dominate WordPress, you can always use the plugin - strangely - called " Automatically set image as featured 1st ", who comes to do the same as the previous code, only You lose the functionality of the default image if there were no images at the entrance.

The best thing about this plugin is that there is nothing to configure, install it, activate it and it works.

0 comments:

Post a Comment