0

I have several posts that have a [gallery] code without any picture IDs shown in the 'text' mode of the post content. Viewing the page shows the pictures in the gallery, but trying to extract the picture IDs within a customized function does not show the pictures. (The custom function - within my own plugin - uses the loop to display content in a specific format from the database; the content is eventually exported to a file for external use.)

The post was created with Postie (plugin that imports post content via an email); the email has pictures attached. (This is not a plugin question.)

The content when viewed in text mode shows something similar to this:

Here is text before the gallery shortcode.[gallery]

I have the get_post_gallery() function within the loop with the sample code shown in the Codex:

$gallery = get_post_gallery( get_the_ID(), false );

But $gallery does not return anything. I need to get the images that are attached to the empty [gallery] shortcode. The do_shortcode() function (against the post content) doesn't give me what I need: I need to create an img tag with the image information (src, href, alt, class,etc).

How do I get the information about the images that are associated with an empty [gallery] tag?

3
  • Maybe you're not having an access to the global $post object within your loop?
    – birgire
    Jan 20, 2018 at 22:19
  • What are you trying to do with this information?
    – Tom J Nowell
    Jan 21, 2018 at 0:04
  • 1) the $post object is available and used; I can export the content. Nothing shows for the gallery shortcode. 2) The purpose is to export the post(s) - content and pictures - without the header/footer/etc of a normally processed page. Jan 21, 2018 at 3:17

1 Answer 1

0

I don't like how WordPress processes the gallery shortcode.

This is some slightly modified code I had that I've used to get the images in a gallery. In this example, the attachments (and thus their IDs) would only be available after the gallery shortcode has been processed.

If you're looking to display different HTML than the default gallery, I'd use the post_gallery filter to short-circuit the built-in WordPress shortcode and display your own.

namespace StackExchange\WordPress;

class Q291678 {
   protected $attachements = [];
   public function getAttachments() : array {
     return $this->attachments;
   }
   public function wp_loaded() {
     \add_filter( 'shortcode_atts_gallery', [ $this, 'shortcode_atts_gallery' ], 10, 4 );
   }
   public function shortcode_atts_gallery( array $out, array $pairs, array $atts, string $shortcode ) : array {
     $this->attachements = $this->getPostAttachments( $out );
     return $out;
   }
   protected function getPostAttachments( array $atts ) : array {
     //* Code copied from WP Core to get post attachements
     $id = intval( $atts[ 'id' ] );
     if ( ! empty( $atts[ 'include' ] ) ) {
         $_attachments = \get_posts( [
           'include'        => $atts[ 'include' ],
           'post_status'    => 'inherit',
           'post_type'      => 'attachment',
           'post_mime_type' => 'image',
           'order'          => $atts[ 'order' ],
           'orderby'        => $atts[ 'orderby' ]
         ] );

         $attachments = [];
         foreach ( $_attachments as $key => $val ) {
             $attachments[ $val->ID ] = $_attachments[ $key ];
         }
     } elseif ( ! empty( $atts['exclude'] ) ) {
         $attachments = \get_children( [
           'post_parent'    => $id,
           'exclude'        => $atts[ 'exclude' ],
           'post_status'    => 'inherit',
           'post_type'      => 'attachment',
           'post_mime_type' => 'image',
           'order'          => $atts[ 'order' ],
           'orderby'        => $atts[ 'orderby' ]
         ] );
     } else {
         $attachments = \get_children( array( 
           'post_parent'    => $id, 
           'post_status'    => 'inherit',
           'post_type'      => 'attachment',
           'post_mime_type' => 'image',
           'order'          => $atts[ 'order' ],
           'orderby'        => $atts[ 'orderby' ] ) );
     }
     //* End code from core
     return $attachments;
   }
}
\add_action( 'wp_loaded', [ new Q291678, 'wp_loaded' ] );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.