• Resolved ace0930

    (@ace0930)


    I’m trying to check if the post content has a gallery, and I’m very sure I have added a gallery via Gutenberg.

    But when I use the below code to check, it tells me that there is no gallery in the post content.

    
    global $post;
    
    if ( ! has_shortcode ( $post -> post_content, 'gallery' ) ) {
      echo 'No gallery';
    }
    

    Can you confirm if this is a bug?

    Or is it possible to check all the shortcodes in the post_content and echo them so I can inspect them further?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Which gallery did you add? The gallery block within Gutenberg is not the same as the [gallery] shortcode. has_shortcode() will find one but not the other. The Gutenberg gallery is not implemented as a shortcode, so extracting any shortcodes in content will not yield the results you want.

    If you want to check for a gallery block in content, you need something like:
    if ( false === strpos( $post->post_content, '<!-- wp:gallery') ) { // etc....

    Thread Starter ace0930

    (@ace0930)

    @bcworkz Thanks, this is working. May I know how you find this <!-- wp:gallery?

    Because in the future I might want to check if there is some other Gutenberg block in the content, but I do not know the specific string/keyword to search for.

    Moderator bcworkz

    (@bcworkz)

    Use Gutenberg’s code editor view to see the raw HTML ?? (from 3 dot icon’s menu at upper right). Every block is demarcated by comments similar to the one for galleries. The tag is typically followed by several attributes which keep track of the block’s user settings and similar data.

    Thread Starter ace0930

    (@ace0930)

    @bcworkz That’s what I’m looking for! Thank you so much!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘has_shortcode cannot catch gallery’ is closed to new replies.