WordPress 函数:has_shortcode

检查内容中是否含有指定的 Shortcode。

用法

<?php if ( has_shortcode( $content, 'gallery' ) ) { } ?>

参数

$content
(string) (required) 要检测的内容
Default: None

$tag
(string) (required) 指定的 Shortcode
Default: None

返回值

(Bool)
指定的 Shortcode 找的返回 true,否则 false。

实例

<?php
$content = 'This is some text, (perhaps pulled via $post->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