- 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細)
関数リファレンス/has shortcode
提供: WordPress Codex 日本語版
コンテントが特定のショートコードを渡すか否かをチェックする関数です。
The short code needs to be registered with add_shortcode() to be recognized.
パラメータ
- $content
- (文字列) (必須) 検索するコンテント
- 初期値: なし
- $tag
- (文字列) (必須) 検索されるショートコード
- 初期値: なし
戻り値
- (真または偽)
- ショートコードがある時 true。ない時 false。
使い方
<?php if ( has_shortcode( $content, 'gallery' ) ) { } ?>
用例
シンプルな例
<?php $content = 'This is some text, (perhaps pulled via $post->post_content). It has a [gallery] shortcode.'; // ↑これはテキストです ($post->post_content から取り込まれたものなど) 。[gallery] ショートコードが含まれています。 if( has_shortcode( $content, 'gallery' ) ) { // コンテンツに [gallery] のショートコードが含まれているため、 true が返される。 } ?>
シンプルな例2
Enqueue some script when some post uses some shortcode.
function custom_shortcode_scripts() { global $post; if( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'custom-shortcode') ) { wp_enqueue_script( 'custom-script'); } } add_action( 'wp_enqueue_scripts', 'custom_shortcode_scripts');
変更履歴
3.6.0にて導入されました。
ソースファイル
has_shortcode() は wp-includes/shortcodes.php
にあります。
関連資料
ショートコード:
do_shortcode(),
add_shortcode(),
remove_shortcode(),
remove_all_shortcodes(),
shortcode_atts(),
strip_shortcodes(),
shortcode_exists(),
has_shortcode(),
get_shortcode_regex(),
wp_audio_shortcode(),
wp_video_shortcode(),
フィルター no_texturize_shortcodes /en
最新英語版: WordPress Codex » Function Reference/has_shortcode (最新版との差分)
関数リファレンス、テンプレートタグ目次もご覧ください。