How to display gallery by given ID


I’m trying to display post gallery by using given post ID in WordPress.

Using this resource, I reached the following code which suppose to display the post gallery by given ID.

    $id = $_GET['id']; //The ID of the post

        $gallery = get_post_gallery( $id, false );
          foreach ( $gallery['src'] AS $src ) {
            <div class="grid-item">
              <img src="<?php echo $src; ?>" alt="gallery image"/>

But unfortunately I receive the following errors:

Warnning: Trying to access array offset on value of type bool

Invalid argument supplied for foreach()

How can I display the entire post gallery using given ID?

Do note I have gallery in my post.

Yotam 3 months 0 Answers 13 views 0

Leave an answer