Function Signature:
has_shortcode
Function Description:
Function Examples:
⚠️ Examples below are generated with GPT-3 once every hour. Do not take them too seriously.
Consider them as some extra input in your learning process - reason about them. Will it work? What could fail?
// Example 1: Check if a post content has a specific shortcode
$post_content = get_the_content();
if ( has_shortcode( $post_content, 'gallery' ) ) {
// Do something if the post content has a gallery shortcode
} else {
// Do something else if the post content does not have a gallery shortcode
}
// Example 2: Check if a specific page template has a shortcode
if ( is_page_template( 'template-custom.php' ) ) {
if ( has_shortcode( get_the_content(), 'contact_form' ) ) {
// Display the contact form if the page template has the contact_form shortcode
} else {
// Display a message if the contact form shortcode is not found in the page content
}
}
// Example 3: Avoid using has_shortcode with a hardcoded shortcode name
$shortcode_name = 'video'; // Define the shortcode name dynamically
if ( has_shortcode( get_the_content(), $shortcode_name ) ) {
// Do something if the post content has the specified dynamic shortcode
} else {
// Do something else if the dynamic shortcode is not found in the post content
}