Pernah mendapatkan masalah dimana gambar unggulan atau featured image di wordpress tidak muncul. Kami disini mendapatkannya beberapa hari ini. Salah satu cara menampilkan gambar unggulan tersebut dengan mengambil langsung dari gambar yang ada di halaman posting adalah dengan memasang script berikut pada file function.php tema yang dipakai.
add_filter(
'the_content'
,
'set_attachment_as_featured_image'
);
function
set_attachment_as_featured_image(
$content
) {
global
$post
;
if
(has_post_thumbnail()) {
// display the featured image
$content
= the_post_thumbnail() .
$content
;
}
else
{
// get & set the featured image
$attachments
= get_children(
array
(
'post_parent'
=>
$post
->ID,
'post_status'
=>
'inherit'
,
'post_type'
=>
'attachment'
,
'post_mime_type'
=>
'image'
,
'order'
=>
'ASC'
,
'orderby'
=>
'menu_order'
));
if
(
$attachments
) {
foreach
(
$attachments
as
$attachment
) {
set_post_thumbnail(
$post
->ID,
$attachment
->ID);
break
;
}
// display the featured image
$content
= the_post_thumbnail() .
$content
;
}
}
return
$content
;
}
Selamat mencoba. ...
Tidak ada komentar:
Posting Komentar