通过页面 ID 获取 Wordpress 画廊图片
Get Wordpress gallery images via page ID
我在 WordPress 中有一个名为 'Gallery' 的页面,页面 ID 为 128,我需要在具有不同 ID 的不同页面上仅显示该页面的图库图像。图片是使用标准的 WordPress 图库功能上传的。
我一直在尝试使用 get_children
和 foreach
循环来实现它,但我似乎无法只从我需要的页面 (ID 128) 获取图库图像.
这是我目前的情况:
$images = get_children( array(
'post_parent' => 128,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'orderby' => 'menu_order',
'order' => 'ASC'
) );
if ( $images ) {
// looping through the images
foreach ( $images as $attachment_id => $attachment ) {
echo wp_get_attachment_image( $attachment_id, 'full' );
}
}
如何在不同的页面上显示来自 WordPress 页面的画廊图片?
这最终由 Nate Allen 在 WordPress 堆栈交换中解决。
这是我的 functions.php:
function na_get_gallery_image_urls( $post_id ) {
$post = get_post($post_id);
// Make sure the post has a gallery in it
if( ! has_shortcode( $post->post_content, 'gallery' ) )
return;
// Retrieve all galleries of this post
$galleries = get_post_galleries_images( $post );
// Loop through all galleries found
foreach( $galleries as $gallery ) {
// Loop through each image in each gallery
foreach( $gallery as $image ) {
echo '<img src="'.$image.'">';
}
}
}
然后在我的页面上调用它:
<?php na_get_gallery_image_urls(128); ?>
128 是附加图库的页面 ID。
我在 WordPress 中有一个名为 'Gallery' 的页面,页面 ID 为 128,我需要在具有不同 ID 的不同页面上仅显示该页面的图库图像。图片是使用标准的 WordPress 图库功能上传的。
我一直在尝试使用 get_children
和 foreach
循环来实现它,但我似乎无法只从我需要的页面 (ID 128) 获取图库图像.
这是我目前的情况:
$images = get_children( array(
'post_parent' => 128,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'orderby' => 'menu_order',
'order' => 'ASC'
) );
if ( $images ) {
// looping through the images
foreach ( $images as $attachment_id => $attachment ) {
echo wp_get_attachment_image( $attachment_id, 'full' );
}
}
如何在不同的页面上显示来自 WordPress 页面的画廊图片?
这最终由 Nate Allen 在 WordPress 堆栈交换中解决。
这是我的 functions.php:
function na_get_gallery_image_urls( $post_id ) {
$post = get_post($post_id);
// Make sure the post has a gallery in it
if( ! has_shortcode( $post->post_content, 'gallery' ) )
return;
// Retrieve all galleries of this post
$galleries = get_post_galleries_images( $post );
// Loop through all galleries found
foreach( $galleries as $gallery ) {
// Loop through each image in each gallery
foreach( $gallery as $image ) {
echo '<img src="'.$image.'">';
}
}
}
然后在我的页面上调用它:
<?php na_get_gallery_image_urls(128); ?>
128 是附加图库的页面 ID。