繁体   English   中英

通过页面ID获取Wordpress画廊图像

[英]Get Wordpress gallery images via page ID

我在WordPress中有一个名为“图库”的页面,页面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。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM