• I have a family site where several people will create vacation posts. For example, a museum visit would be a single post with multiple pictures or a gallery. On the main page people will primarily find these by looking/filtering through the images in those posts. I want to create an experience similar to this: [ redundant link removed ]

    /FilterableProductGrid/ but with these photos. I don’t want to use a plug in for this, I want to make it myself.
    It looks like the get_post_galleries_images function is meant for just this sort of thing, but I can’t get it to pull up any image source uri.

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); 
    
    $galleries = get_post_galleries_images( the_id() );
    
    foreach($galleries as $image) {
      echo $result, '<br>';
    }
    ?>

    What am I doing wrong? Should I be taking a different approach (again, I don’t want to use a plugin. I want users to easily be able to create posts and attach images via the WP app).
    Thanks for your help!!!

    The page I need help with: [log in to see the link]

Viewing 8 replies - 1 through 8 (of 8 total)
  • The main thing is you call the_id(), which echoes the ID instead of passing it to the function.
    The second thing is your foreach has $galleries and $images, but your are echoing $result.

    Thread Starter zarwell

    (@zarwell)

    Thanks Joy, I was in the middle of making changes when I pasted the code so the second one wasn’t really a problem, my bad on that one. On the first, I’ve tried passing in several things, including the_post() with no success. What should be passed in?

    Most of the functions that start with the_ will echo their result (not the_post()).
    You can use get_the_id() instead.

    Thread Starter zarwell

    (@zarwell)

    Thanks again Joy. It’s still not working, nothing is being output. I feel bad taking up your time without much to work off of. I’m going to keep digging and I’ll post an answer for others if/when I find it. Below is the current state of my code:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); 
    $galleries = get_post_galleries_images( get_the_id() );
    
    foreach($galleries as $image) {
      echo '<img src="',$image,'" />';
    }
    ?>
    
    <?php get_template_part( 'entry' ); ?>
    <?php comments_template(); ?>
    
    <?php endwhile; endif; ?>
    • This reply was modified 4 years, 8 months ago by bcworkz. Reason: code fixed
    Moderator bcworkz

    (@bcworkz)

    If your server is case sensitive (*nix based systems), you may need to use get_the_ID() (upper case ‘ID’) instead. “id” !== “ID” on *nix systems.

    When you post code in these forums, please demarcate with backticks or use the code button. When you don’t do that the forum’s parser corrupts your code, making it difficult for others to test your code. I fixed your last reply for you. Assuming it’s now what you really have, the echo line will cause HTML syntax errors. I think you want
    echo '<img src="' . $image . '" />';
    or
    echo "<img src=\"$image\" />";

    Thread Starter zarwell

    (@zarwell)

    Thanks for the help. I still can’t see what’s wrong here. I have posts with galleries and with images and no images or are showing with this code, just the title, poster, date and message.

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); 
    
    $galleries = get_post_galleries_images( get_the_ID() );
    
    foreach($galleries as $image) {
      echo '<img src="' . $image . '" />';
    }
    ?>
    
    <?php get_template_part( 'entry' ); ?>
    <?php comments_template(); ?>
    
    <?php endwhile; endif; ?>
    • This reply was modified 4 years, 8 months ago by zarwell.
    Moderator bcworkz

    (@bcworkz)

    There’s nothing wrong with your code, get_post_galleries_images() only supports the old style gallery shortcode, not the block editor gallery ?? I’ve submitted a user note for the pertinent docs pages noting this so it’s at least documented somewhere. It’ll take a while for the note to be approved and appear in the docs.

    You could manually convert the block gallery to shortcode or use the Classic Editor plugin to regain the ability to insert old style galleries. To manually convert, use the block editor code view. The image IDs are listed in the gallery block’s comment. For example:
    <!-- wp:gallery {"ids":[123,456,789]} -->
    Change this to look like this:
    <p>[gallery ids="123,456,789"]</p>
    and delete the remainder of the gallery block code. This does entail removing a lot of content. The block gallery contains all the gallery HTML while the shortcode gallery determines the HTML on the fly upon output using the provided IDs.

    Alternately, write your own function to extract gallery srcs from the block’s content. While this takes greater initial effort, in the long run it’s probably preferable to the alternatives.

    Thread Starter zarwell

    (@zarwell)

    Thanks so much for that. After knowing that I found this and added it to my theme’s function.php. Things are working perfectly now with block gallery.

    function get_post_block_galleries_images( $post_id ) {
        $content = get_post_field( 'post_content', $post_id );
        $srcs = [];
    
        $i = -1;
        foreach ( parse_blocks( $content ) as $block ) {
            if ( 'core/gallery' === $block['blockName'] ) {
                $i++;
                $srcs[ $i ] = [];
    
                preg_match_all( '#src=([\'"])(.+?)\1#is', $block['innerHTML'], $src, PREG_SET_ORDER );
                if ( ! empty( $src ) ) {
                    foreach ( $src as $s ) {
                        $srcs[ $i ][] = $s[2];
                    }
                }
            }
        }
    
        return $srcs;
    }
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Creating a custom gallery by pulling images from post galleries’ is closed to new replies.