Wordpress 3.5: own gallery with included images doesn't work

℡╲_俬逩灬. 提交于 2019-11-28 12:46:39

I figured it out: If you use a gallery with images, which are already uploaded to the media library, the gallery shortcode looks like [gallery ids=1,2,3], what means, that images are only linked (and not attached) to the gallery, so post_type=attachment doesn't work.

Now i'm using regular expressions to get the image IDs:

$post_content = $post->post_content;
preg_match('/\[gallery.*ids=.(.*).\]/', $post_content, $ids);
$array_id = explode(",", $ids[1]);

It's now possible to pull all the galleries or even a single gallery using the $post->ID and get_post_galleries(). Each gallery will contain the image id list in ids as well as a list of the image urls in src. The gallery object is basically the shortcode arguments so you have access to those as well.

if ( $galleries = get_post_galleries( $post->ID, false ) ) {

    $defaults = array (
        'orderby'    => 'menu_order ASC, ID ASC',
        'id'         => $post->ID,
        'itemtag'    => 'dl',
        'icontag'    => 'dt',
        'captiontag' => 'dd',
        'columns'    => 3,
        'size'       => 'full',
        'link'       => 'file',
        'ids'        => "",
        'src'        => array (),
    );

    foreach ( $galleries as $gallery ) {

        // defaults
        $args = wp_parse_args( $gallery, $defaults );

        // image ids
        $args[ 'ids' ] = explode( ',', $args[ 'ids' ] );

        // image urls
        $images = $args[ 'src' ];
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!