概要
tec_events_get_current_view
関数は、WordPress プラグイン「The Events Calendar」において、現在表示されているイベントのビューを取得するために使用されます。この関数は、ユーザーインターフェースの状況に応じて、適切なカスタマイズを行う際や、特定の条件に応じてコードを程よく調整する際に有用です。以下の機能を実装する際によく使われます。
- カレンダーの表示形式の変更
- イベントリストのフィルタリング
- 特定のイベントに対するカスタムスタイルの適用
- ウィジェットエリアに表示するイベントのカスタマイズ
- ショートコードを使ったイベント表示
- 他のプラグインとの連携によるイベント情報の表示
構文
tec_events_get_current_view();
パラメータ
この関数は引数を取らないため、特にオプションのパラメータはありません。
戻り値
この関数は、現在のビューの情報を配列で返します。ビューには、「month」や「list」など、イベントが表示される方法が示されます。
プラグインとWordPressのバージョン
- The Events Calendar バージョン: 6.0以降
- WordPress バージョン: 5.0以降
この関数のアクションでの使用可能性
アクション | 使用例 |
---|---|
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
$current_view = tec_events_get_current_view();
if ($current_view['view'] === 'month') {
// 月表示の時のみ適用
add_filter('the_content', function($content) {
return $content . '<p>月表示の特別なお知らせ</p>';
});
}
このコードは、現在の表示が「月」ビューの場合に、コンテンツの末尾に特別なお知らせを追加します。
サンプルコード2
$current_view = tec_events_get_current_view();
// イベントをリスト表示に変換
if ($current_view['view'] === 'list') {
remove_action('the_content', 'event_content_function');
add_action('the_content', 'custom_event_list_display');
}
このコードは、イベント情報の表示形式を変更するために、リスト表示中に既存のアクションを削除し、新しくカスタムの表示関数を追加します。
サンプルコード3
$current_view = tec_events_get_current_view();
?>
<div class="current-view">
<p>現在のビュー: <?php echo esc_html($current_view['view']); ?></p>
</div>
<?php
このコードは、現在のイベントビューを画面に表示するためのシンプルな HTML 出力を行います。
サンプルコード4
$current_view = tec_events_get_current_view();
if ($current_view['view'] === 'day') {
// 特定のスタイルを適用
wp_enqueue_style('day-view-style', get_stylesheet_directory_uri() . '/css/day-view.css');
}
このコードは、「日」ビューに対応する特別なスタイルシートを読み込みます。
サンプルコード5
$current_view = tec_events_get_current_view();
// イベントのフィルタリング
add_filter('tec_events_filter', function($events) use ($current_view) {
if ($current_view['view'] === 'month') {
return array_filter($events, function($event) {
return strtotime($event->date) > time(); // 現在以降のイベントのみ
});
}
return $events;
});
このコードは、「月」ビューの際にのみ、現在以降のイベントのみを取得するためのフィルタを追加します。