Detect if is image unattached

For unattached images, the post_parent column in the wp_posts table, has the value of 0.

Your gallery shortcode in that case is:



which means you are calling get_children() with post_parent as 0.

You can e.g. use the gallery shortcode callback gallery_shortcode() directly:

if( $parent_id = $post->post_parent )
{
    echo gallery_shortcode( 
        [ 
            'id'      => (int) $parent_id, 
            'columns' => 4 
        ] 
    );
}

where we only display the gallery for attached images.

Leave a Comment