概要
is_embed
関数は、要求されているページが埋め込み投稿ページかどうかを調べるための非常に便利な関数です。この関数は、特に以下のような機能を実装する際に利用されることが一般的です。
- 埋め込みコンテンツのスタイルを条件によって変える
- 埋め込み投稿専用のスクリプトやスタイルをキューに追加
- 特定の埋め込みコンテンツに対するカスタムロジックを実行
- SEO用のメタタグを埋め込みページ用に調整
- 埋め込み投稿に対するフィルタやアクションを実行
- ウィジェットエリアの表示条件を調整
- ナビゲーションメニューの表示内容を条件によって変更
- 埋め込みページ専用のサイドバーを表示
構文
is_embed();
パラメータ
この関数にはパラメータはありません。
戻り値
is_embed
関数は、現在のリクエストが埋め込み投稿である場合に true
を、そうでない場合に false
を返します。
関連する関数
使用可能なバージョン
is_embed
関数は、WordPress 3.2.0 以降で使用可能です。
コアファイルのパス
wp-includes/query.php
この関数のアクションでの使用可能性
アクション | 使用可能性 |
---|---|
mu_plugin_loaded | |
registered_post_type | |
plugins_loaded | |
wp_roles_init | |
setup_theme | |
after_setup_theme | |
set_current_user | |
init | |
register_sidebar | |
wp_loaded | |
send_headers | |
parse_query | |
pre_get_posts | |
wp | |
template_redirect | 〇 |
get_header | |
wp_head |
サンプルコード
サンプル1: 埋め込み投稿専用のスタイルを追加
このコードは、埋め込み投稿にのみ特定のスタイルを追加する例です。
function enqueue_embed_styles() {
if (is_embed()) {
wp_enqueue_style('embed-style', get_template_directory_uri() . '/css/embed.css');
}
}
add_action('wp_enqueue_scripts', 'enqueue_embed_styles');
(出典: https://developer.wordpress.org/reference/functions/wp_enqueue_style/)
サンプル2: 埋め込みコンテンツを条件付けて読み込む
このコードは、埋め込み投稿の場合に特定のスクリプトを追加します。
function add_embed_script() {
if (is_embed()) {
wp_enqueue_script('embed-script', get_template_directory_uri() . '/js/embed.js', array(), null, true);
}
}
add_action('wp_enqueue_scripts', 'add_embed_script');
(出典: https://developer.wordpress.org/reference/functions/wp_enqueue_script/)
サンプル3: 埋め込みページ専用のサイドバーを表示する
このコードは、埋め込み投稿の際に特定のサイドバーを表示するためのものです。
function show_embed_sidebar() {
if (is_embed()) {
get_sidebar('embed');
}
}
add_action('get_sidebar', 'show_embed_sidebar');
(出典: https://developer.wordpress.org/reference/functions/get_sidebar/)
サンプル4: SEO用メタタグの調整
このコードは、埋め込み投稿用にSEOメタタグを追加する例です。
function add_meta_tags_for_embed() {
if (is_embed()) {
echo '<meta name="robots" content="noindex, follow">';
}
}
add_action('wp_head', 'add_meta_tags_for_embed');
(出典: https://developer.wordpress.org/reference/functions/add_action/)
サンプル5: 埋め込み投稿に特有のフィルタを実装
このコードは、埋め込み投稿のコンテンツにフィルタを適用する例です。
function modify_embed_content($content) {
if (is_embed()) {
$content .= '<p>この投稿は埋め込まれています。</p>';
}
return $content;
}
add_filter('the_content', 'modify_embed_content');
(出典: https://developer.wordpress.org/reference/functions/add_filter/)