WordPress otomatik öne çıkan görsel kodu

WordPress autoset featured image kodu

WordPress otomatik öne çıkan görsel kodu : WordPress sitelerinizde içeriğe eklenen ilk görseli featured image ve anasayfada thumbnail olarak ayarlamanıza yarayan kod ile birlikte eklentisiz bir şekilde bu sorunu çözebilirsiniz.

Bu işlemi yaptığınızda yazı içerisine eklenen resimler otomatik olarak featured image olarak ayarlanıp anasayfada ise ilgili yazının küçük resmi (thumbnail) olur.

Bu kodu functions.php sayfasının en alt kısmına eklemeniz gerekmektedir.

// Auto add featured image
function wpsites_auto_set_featured_image() {
   global $post;
   $featured_image_exists = has_post_thumbnail($post->ID);
      if (!$featured_image_exists)  {
         $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', 'wpsites_auto_set_featured_image');

İlk yorum yapan olun

Bir yanıt bırakın

E-posta hesabınız yayımlanmayacak.


*