【问题标题】:Get Wordpress gallery images via page ID通过页面 ID 获取 Wordpress 图库图片
【发布时间】:2015-10-06 00:37:18
【问题描述】:

我在 WordPress 中有一个名为“Gallery”的页面,页面 ID 为 128,我只需要在具有不同 ID 的不同页面上显示来自该页面的画廊图像。图片是使用标准的 WordPress 图库功能上传的。

我一直在尝试使用get_childrenforeach 循环来实现它,但我似乎无法仅从我需要的页面(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 页面的图库图片?

【问题讨论】:

    标签: php wordpress gallery


    【解决方案1】:

    这最终在 WordPress 堆栈交换中由 Nate Allen 解决。

    这是在我的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。

    【讨论】:

      猜你喜欢
      • 2015-03-20
      • 2016-02-04
      • 1970-01-01
      • 2014-08-17
      • 2017-04-09
      • 1970-01-01
      • 2018-03-15
      • 1970-01-01
      • 2011-11-08
      相关资源
      最近更新 更多