检查内容中是否含有指定的 shortcode。
用法
参数
$content
(string) (required) 要检测的内容
default: none
$tag
(string) (required) 指定的 shortcode
default: none
返回值
(bool)
指定的 shortcode 找的返回 true,否则 false。
实例
post_content). it has a [gallerys] shortcode.';
if( has_shortcode( $content, 'gallerys' ) ) {
// 内容中有 [gallerys] shortcode,将返回 true。
}
?>
在内容中含有指定的 shotcode 的时候才载入相应的脚本。
function custom_shortcode_scripts() {
global $post;
if( has_shortcode( $post->post_content, 'custom-shortcode') ) {
wp_enqueue_script( 'custom-script');
}
}
add_action( 'wp_enqueue_scripts', 'custom_shortcode_scripts');
修改记录
since 3.6.0
源文件
wp-includes/shortcodes.php