has_shortcode

函数
has_shortcode ( $content, $tag )
参数
  • (string) $content Content to search for shortcodes.
    Required:
  • (string) $tag Shortcode tag to check.
    Required:
返回值
  • (bool) Whether the passed content contains the given shortcode.
定义位置
相关方法
add_shortcodedo_shortcodestrip_shortcodesapply_shortcodesgallery_shortcode
引入
3.6.0
弃用
-

has_shortcode – 这是一个WordPress函数,用于检查一个文章或页面的内容中是否存在特定的短码。短码是自定义的代码片段,可以用来将复杂的功能或内容嵌入到一个文章或页面中。has_shortcode函数需要两个参数:第一个是简码的名称,第二个是文章或页面的内容。

判断传递的内容是否包含指定的短码。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function has_shortcode( $content, $tag ) {
if ( false === strpos( $content, '[' ) ) {
return false;
}
if ( shortcode_exists( $tag ) ) {
preg_match_all( '/' . get_shortcode_regex() . '/', $content, $matches, PREG_SET_ORDER );
if ( empty( $matches ) ) {
return false;
}
foreach ( $matches as $shortcode ) {
if ( $tag === $shortcode[2] ) {
return true;
} elseif ( ! empty( $shortcode[5] ) && has_shortcode( $shortcode[5], $tag ) ) {
return true;
}
}
}
return false;
}
function has_shortcode( $content, $tag ) { if ( false === strpos( $content, '[' ) ) { return false; } if ( shortcode_exists( $tag ) ) { preg_match_all( '/' . get_shortcode_regex() . '/', $content, $matches, PREG_SET_ORDER ); if ( empty( $matches ) ) { return false; } foreach ( $matches as $shortcode ) { if ( $tag === $shortcode[2] ) { return true; } elseif ( ! empty( $shortcode[5] ) && has_shortcode( $shortcode[5], $tag ) ) { return true; } } } return false; }
function has_shortcode( $content, $tag ) {
	if ( false === strpos( $content, '[' ) ) {
		return false;
	}

	if ( shortcode_exists( $tag ) ) {
		preg_match_all( '/' . get_shortcode_regex() . '/', $content, $matches, PREG_SET_ORDER );
		if ( empty( $matches ) ) {
			return false;
		}

		foreach ( $matches as $shortcode ) {
			if ( $tag === $shortcode[2] ) {
				return true;
			} elseif ( ! empty( $shortcode[5] ) && has_shortcode( $shortcode[5], $tag ) ) {
				return true;
			}
		}
	}
	return false;
}

常见问题

FAQs
查看更多 >