How to create galleries for wordpress posts withou

2019-09-01 17:32发布

问题:

I want to create galleries for my posts working like this :

  1. I choose pictures in wordpress post-new page and upload them
  2. Wordpress creates an array of those images uploaded
  3. And finally i use that array and put gallery into my single.php file

Actually woocommerce is using the exact thing as you see in the picture ( sorry i didn't have English wordpress installed )

any suggestions?

回答1:

you can use the default gallery behavior to your need

  • Insert a gallery in tour post, it will create a shortcode like

    [gallery ids="12,45,67,34"]
    
  • override the default gallery shortcode function to not display it in your post

    remove_shortcode( 'gallery', 'gallery_shortcode' );
    add_shortcode( 'gallery', 'my_gallery');
    function my_gallery(){
        return '';
    }
    
  • retrieve an array of images ids

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