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函数需要两个参数:第一个是简码的名称,第二个是文章或页面的内容。

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

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
查看更多 >
闪电侠

(工作日 10:00 - 18:30 为您服务)

2025-12-05 18:32:58

您好,无论是售前、售后、意见建议……均可通过联系工单与我们取得联系。

您也可选择聊天工具与我们即时沟通或点击查看:

您的工单我们已经收到,我们将会尽快跟您联系!
取消
选择聊天工具: