Web
Analytics

Membuat Website Berbasis WordPress Auto Set Featured Image Biar Lebih Cantik

Auto Set Featured Image
Hari ini website saya yang lain ganti theme biar lebih bagus, sebenernya tampilannya udah bagus, tp sayangnya diside bar yang biasanya munculin image yang digenerate dari set featured image, ga nongol lagi disamping website saya, tp untungnya saya masih simpan contekan supaya bisa buat set featured image nya menjadi auto tanpa bantuan plugin, jadi bisa lebih irit resource server.

Oke step-stepnya sebagai berikut, copy code dibawah ini dan paste sebelum tanda “}” terakhir, bisa dimana aja yang penting nantinya tanda } akan menutup codenya, sorry bahasanya ga begitu bagus, karena ini sebenarnya untuk kepentingan saya sendiri sih biar ingat kalau saya mau pakai.

So ini dia codenya, dipaste di theme yang dipakai/fucntions.php

// Inside your functions file add the following code
//
function wpforce_featured() {
global $post;
$already_has_thumb = has_post_thumbnail($post->ID); // If post have a featured image use that.
if (!$already_has_thumb) {
// If post does not have a featured image then get the first post image and set as featured image.
$attached_image = get_children( “post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1” ); // Number 1 relates to taking post image number 1 and adding it as a featured image.
if ($attached_image) {
foreach ($attached_image as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, $attachment_id);
//$attachment_id = attachment_url_to_postid( $image_url );
// echo $attachment_id;

}
} else if ( in_category(‘WordPress’) ){ // Add your own categories.
set_post_thumbnail($post->ID, ’21’);
// Find attachment media id by right clicking the image in the Media library and selecting inspect element. Look for the data-id number. This number is then added to the post id.
}

else if ( in_category(‘test’) ) {
set_post_thumbnail($post->ID, ’30’);
}
else if ( in_category(‘images’) ) {
set_post_thumbnail($post->ID, ‘111’);
}
else if ( in_category(‘Uncategorized’) ) {
set_post_thumbnail($post->ID, ‘111’);
}
}
} //end function
add_action(‘the_post’, ‘wpforce_featured’);
add_action(‘save_post’, ‘wpforce_featured’);
add_action(‘draft_to_publish’, ‘wpforce_featured’);
add_action(‘new_to_publish’, ‘wpforce_featured’);
add_action(‘pending_to_publish’, ‘wpforce_featured’);
add_action(‘future_to_publish’, ‘wpforce_featured’);

Itu aja deh semoga bisa membantu.

Add a Comment

Your email address will not be published. Required fields are marked *