Get images from post's gallery WordPress 3.5

随声附和 提交于 2020-01-01 07:20:17

问题


How to get images from a gallery in a post in WordPress 3.5 as gallery is no longer related to posts in 3.5. get_children() doesnot work as gallery is not attachment. Any help is appreciated.


回答1:


You must probably parse shortcode:

http://codex.wordpress.org/Gallery_Shortcode

Use regular expression:

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



回答2:


`global $post;
    $post_subtitrare = get_post( $post->ID );
    $content = $post_subtitrare->post_content;
    $pattern = get_shortcode_regex();
    preg_match( "/$pattern/s", $content, $match );
    if( isset( $match[2] ) && ( "gallery" == $match[2] ) ) {
        $atts = shortcode_parse_atts( $match[3] );
        $attachments = isset( $atts['ids'] ) ? explode( ',', $atts['ids'] ) : get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID .'&order=ASC&orderby=menu_order ID' );
    }`

The $attachments will get you what you are used to getting prior to WordPress 3.5.



来源:https://stackoverflow.com/questions/13873532/get-images-from-posts-gallery-wordpress-3-5

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