是否有一个函数可以让我获取第一个子级的帖子id(属于相同的帖子类型,而不是附件)?我总是可以通过wp\\u查询运行一个循环,但这似乎有些过头了/
这里的目标是构建一个显示第一个附加图像的函数My function会遍历父贴子的post\\u缩略图和附加图像,但当只有图像位于子贴子中时会停止工作罢工>
Edit: Here is my current working function. It works, but its not pretty. How can I clean it up?
/****************************************/
/* Grab first image of a post
/****************************************/
function surfbird_grab_album_image() {
global $post, $posts;
$albumtitle = get_the_title();
if(has_post_thumbnail()) :
return the_post_thumbnail(\'small\');
else :
//Grab image if parent post has one
$args = array(
\'order\' => \'ASC\',
\'orderby\' => \'menu_order\',
\'post_type\' => \'attachment\',
\'post_parent\' => $post->ID,
\'post_mime_type\' => \'image\',
\'numberposts\' => 1,
);
$attachments = get_posts($args);
if ($attachments) :
foreach ($attachments as $attachment) {
echo wp_get_attachment_image($attachment->ID, \'small\', false, false);
echo apply_filters( \'the_title\', $albumtitle );
}
else :
//Search the child posts and grab the first image we find
$albumgroups = get_posts( array( \'post_type\' => \'album\', \'post_parent\' => get_the_ID(), \'orderby\' => \'menu_order\', \'numberposts\' => -1 ) );
foreach( $albumgroups as $post ) : setup_postdata($post);
$postID = $post->ID;
$args = array(
\'numberposts\' => 1,
\'order\'=> \'ASC\',
\'post_mime_type\' => \'image\',
\'post_parent\' => $postID,
\'post_status\' => null,
\'post_type\' => \'attachment\'
);
$attachments = get_children( $args );
if ($attachments) {
foreach($attachments as $attachment) {
return wp_get_attachment_image($attachment->ID, \'small\', false, false);
}
}
endforeach;
wp_reset_query();
endif;
endif;
wp_reset_postdata();
}
SO网友:Tom J Nowell
而不是:
$args = array(
\'order\' => \'ASC\',
\'orderby\' => \'menu_order\',
\'post_type\' => \'attachment\',
\'post_parent\' => $post->ID,
\'post_mime_type\' => \'image\',
\'numberposts\' => 1,
);
使用:
$args = array(
\'order\' => \'ASC\',
\'orderby\' => \'menu_order\',
\'post_type\' => get_post_type($post),
\'post_parent\' => $post->ID,
\'numberposts\' => 1,
);
您也可以使用post/page/etc或“any”