飞龙博客

feilong.org 前端开发教程

wordpress theme get first attachment img function

飞龙修订于 20101121 07:53 39 次浏览

How to get first attachment img in a wordpress theme files eg index.php?Here feilong give you one resolution.

 used in a loop 20101121 shenzhen
$args = array(
'order'          => 'ASC',
'post_type'      => 'attachment',
'post_parent'    => $post->ID,//feilong for current post in a loop
'post_mime_type' => 'image',
'post_status'    => null,
'numberposts'    => 1,
);$attachments = get_posts($args);if ($attachments) {
$feilong_attach_first_img = wp_get_attachment_image($attachments[0]->ID, 'thumbnail', false);
}
?>

Of course,you can put it in functions.php too.Then use this function somewhere in your theme.


function feilong_get_attach_first_img($postid,$size='thumbnail') {
$args = array(
'order'          => 'ASC',
'post_type'      => 'attachment',
'post_parent'    => $postid,
'post_mime_type' => 'image',
'post_status'    => null,
'numberposts'    => 1,
);$attachments = get_posts($args);if ($attachments) {
$feilong_attach_first = wp_get_attachment_image($attachments[0]->ID, $size, false);
return $feilong_attach_first;}
}
//then somewhere in your loop of wordpress theme  
echo $feilong_get_attach_first_img($post->ID);
?>

There are other ways to display wordpress attachment.Perhaps wp_get_attachment_image_src() is a more free wordpress function for you to code and resolve attachment problems.For more,contact me.

网址:https://feilong.org/wordpress-theme-get-first-attachment-img-function
初发:20101121 07:53

想学前端?长按二维码加我微信

想学前端?点击飞龙QQ

分类: wordpress

新作:

旧文:

关闭

长按二维码 -> 识别图中二维码

打开微信,发现,扫一扫

支付完成后,点击下一步

感谢大力支持

然后你手机号:

最后点这,稍等即可