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
檢視更多 >