How to create galleries for wordpress posts without plugin

北城余情 提交于 2019-12-25 02:40:45

问题


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]);
    }
    


来源:https://stackoverflow.com/questions/21828906/how-to-create-galleries-for-wordpress-posts-without-plugin

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