在我的页面上,图库图像是循环的,我想将它们的标题显示为标题。自从get_post_gallery_images
仅检索我尝试使用的库图像的缩略图urlget_attached_media(\'image\', $post);
相反
以下代码为我提供了库图像的url和标题:
foreach($images as$image){$titleArr[]=$image->post\\u title;$urlArr[]=wp\\u get\\u attachment\\u url($image->ID);}
然而get_attached_media(\'image\', $post);
返回曾经附加到此帖子的所有图像,即使这些图像不再在帖子中使用。此外,对于库图像附加到其他帖子的帖子,它们不会显示。
是否有get_attached_media(\'image\', $post);
返回所有图像URL和图像标题currently 在库中。最好按画廊顺序。
SO网友:Eirik
使用快捷码找到答案,这有助于:How to get Page/Post Gallery attachment images in order they are set in backend using WP_Query()?
我的工作代码现在如下所示:
// Extract the shortcode arguments from the $page or $post
$shortcode_args = shortcode_parse_atts(get_match(\'/\\[gallery\\s(.*)\\]/isU\', $post->post_content));
// get the ids specified in the shortcode call
$ids = $shortcode_args["ids"];
$attachments = get_posts(
array(
\'include\' => $ids,
\'post_status\' => \'inherit\',
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image\',
\'order\' => \'menu_order ID\',
\'orderby\' => \'post__in\', //required to order results based on order specified the "include" param
)
);
if ($attachments) {
foreach ( $attachments as $attachment ) {
$urlArr[] = wp_get_attachment_url($attachment->ID);
$titleArr[] = $attachment->post_title;
}
}