概要
elementor/utils/is_post_type_support
関数は、特定の投稿タイプが指定された機能をサポートしているかどうかを確認するためのユーティリティ関数です。この関数は、さまざまなカスタム投稿タイプや投稿編集ページでの機能提供に役立ち、特に以下のような機能を実装する際に利用されることが多いです。
- カスタム投稿タイプの追加
- 特定のメタボックスの表示切り替え
- カスタムフィールドのサポート
- 投稿形式の利用
- タクソノミーの統合
- カスタムクエリによるデータ取得
構文
is_post_type_support( $post_type, $feature );
パラメータ
$post_type
: チェックしたい投稿タイプ(文字列)$feature
: 確認したい機能(文字列)
戻り値
- 指定した投稿タイプが特定の機能をサポートしている場合は
true
、そうでなければfalse
を返します。
使用可能なプラグインバージョン
- Elementor を利用している場合のバージョンは、2.0以上です。
使用可能な WordPress バージョン
- WordPress 4.0 以上。
サンプルコード
サンプル1: カスタム投稿タイプに機能がサポートされているか確認する
$post_type = 'custom_post';
$feature = 'thumbnail';
if ( is_post_type_support( $post_type, $feature ) ) {
echo 'この投稿タイプはアイキャッチ画像をサポートしています。';
} else {
echo 'この投稿タイプはアイキャッチ画像をサポートしていません。';
}
// 機能のサポートをチェックする基本的な例です。
サンプル2: 投稿タイプによるメタボックスの制御
add_action( 'add_meta_boxes', function() {
if ( is_post_type_support( 'book', 'author' ) ) {
add_meta_box( 'book_author', 'Author', 'book_author_callback', 'book' );
}
});
// カスタム投稿タイプ 'book' に 'author' 機能がある場合、メタボックスを追加します。
サンプル3: 投稿の種類によってスタイルを変更
function custom_styles_for_post_type() {
if ( is_post_type_support( get_post_type(), 'editor' ) ) {
wp_enqueue_style( 'editor-style', get_template_directory_uri() . '/editor-style.css' );
}
}
add_action( 'wp_enqueue_scripts', 'custom_styles_for_post_type' );
// 投稿が 'editor' 機能をサポートしている場合にスタイルシートを読み込みます。
サンプル4: タクソノミー追加の条件チェック
if ( is_post_type_support( 'product', 'taxonomies' ) ) {
register_taxonomy( 'product_cat', 'product', array( 'hierarchical' => true, 'label' => 'Product Categories' ) );
}
// 'product' 投稿タイプがタクソノミーをサポートしている場合、タクソノミーを登録します。
サンプル5: 特定のフィーチャーによる投稿のフィルタリング
function filter_posts_by_feature( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {
if ( is_post_type_support( 'portfolio', 'thumbnail' ) ) {
$query->set( 'meta_key', 'portfolio_images' );
}
}
}
add_action( 'pre_get_posts', 'filter_posts_by_feature' );
// 投稿が 'thumbnail' フィーチャーをサポートしている場合、メタクエリを設定します。
この関数のアクションでの使用可能性
アクション | 使用可能性 |
---|---|
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 |
この表は、elementor/utils/is_post_type_support
関数がさまざまなアクションフックでどのように使用されるかを示しています。特に、registered_post_type
と pre_get_posts
での使用が確認されています。