Just use the public API functions: has_shortcode( $content, $tag );
. It uses shortcode_exists( $shortcode );
internally to search the global $shortcode_tags
array for the shortcode you are searching for. If that is successful, it uses get_shortcode_regex()
to search for the actual shortcode – this will save you quite some time and avoid mistakes.
the_title();
if ( has_shortcode( get_the_content(), 'gallery' ) )
{
the_content();
}
else
{
// show default view - example:
the_post_thumbnail( 'your-desired-size' );
the_content();
}
The gallery_shortcode( $post_id );
function might be of help as well.
This might not work if you are using a plugin that handles galleries different from how WP core does it. Anyway, make sure that you alter above has_shortcode()
call to use the shortcode you are (or the plugin you are using) is using.