質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

配列

配列は、各データの要素(値または変数)が連続的に並べられたデータ構造です。各配列は添え字(INDEX)で識別されています。

Q&A

0回答

1240閲覧

WordPressで自作のイベントカレンダーに祝日の表示をさせたい

serizawa3

総合スコア16

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

配列

配列は、各データの要素(値または変数)が連続的に並べられたデータ構造です。各配列は添え字(INDEX)で識別されています。

0グッド

1クリップ

投稿2021/02/19 17:21

#前提・実現したいこと
お世話になります。
WordPressに自作のイベントカレンダーを設置させて、そのカレンダーに祝日の表示をさせたいと思い、
GoogleカレンダーAPIを取り入れ、祝日の取得はできているようなのですが、カレンダーに反映することができませんでした。

エラーの表示が出ておらず、どこがいけないのかが分からない状態です。
どなたかご教授いただけますでしょうか。
どうぞよろしくお願いいたします。

#参考にしたサイト

https://yahss.net/wordpress/1316-event-calendar/
https://www.asobou.co.jp/blog/web/googlecalender2

##祝日を取得するためのコード

php

1function get_holidays ($ym, $t) { 2 3 $holidays = get_transient( 'holidays'.$ym ); 4 if ( !$holidays ) { 5 $api_key = '取得したAPIキーを入力しています'; 6 $calendar_id = urlencode('japanese__ja@holiday.calendar.google.com'); 7 // データの開始日 8 $start = date('2020-01-01\T00:00:00\Z'); 9 // データの終了日 10 $end = date('2022-12-31\T00:00:00\Z'); 11 $url = "https://www.googleapis.com/calendar/v3/calendars/" . $calendar_id . "/events?"; 12 $query = [ 13 'key' => $api_key, 14 'timeMin' => $start, 15 'timeMax' => $end, 16 'maxResults' => 50, 17 'orderBy' => 'startTime', 18 'singleEvents' => 'true' 19 ]; 20 $holidays = []; 21 if ($data = file_get_contents($url. http_build_query($query), true)) { 22 $data = json_decode($data); 23 //年月日をキー、祝日名を配列に格納 24 foreach ($data->items as $item ) { 25 $date = strtotime((string) $item->start->date); 26 $title = (string) $item->summary; 27 $holidays[date('Y-m-d', $date)] = $title; 28 } 29 } 30 set_transient( 'holidays'.$ym, $holidays, 3600 * 24 ); 31 } 32return $holidays; 33}

##カレンダーの表示

php

1function shortcode_my_calendar($params = array()) { 2 3 // ショートコードのパラメータを取得 4extract(shortcode_atts(array( 5 'ym' => null 6), $params)); 7 8 // 現在の年月日を取得 9 $current_y = date_i18n('Y'); 10 $current_m = date_i18n('m'); 11 $current_d = date_i18n('d'); 12 $params = array(); 13 // ショートコードで年月が指定していなければ、現在の年月を設定 14 15 $calendar_ym = ( $params['ym'] ) ? $params['ym'] : $current_y . '-' . $current_m; 16 17 // 該当月のイベントを取得し配列に格納 18 $args = array( 19 'post_status' => 'publish', 20 'posts_per_page' => -1, // 全件取得 21 'meta_query' => array( 22array( 23 'key' => 'event_date', 24 'value' => $calendar_ym, 25 'compare' => 'LIKE' 26) 27 ) 28 ); 29 $event_posts = get_posts( $args ); 30 $events = array(); 31 if ( $event_posts ) { 32 foreach ( $event_posts as $post ) { 33$event_date = esc_html( get_post_meta( $post -> ID, 'event_date', true ) ); 34$event_link = get_permalink( $post -> ID ); 35$events[$event_date][] = "<a href='{$event_link}'></a>"; 36 } 37 } 38 39 // 表示年月の日数を取得 40 $calendar_t = date_i18n('t', strtotime($calendar_ym.'-01')); 41 42 // 祝日の取得 43 $holidays = get_holidays ($calendar_ym, $calendar_t); 44 45 // カレンダー表示 ?> 46<h3><?php echo $calendar_ym ?></h3> 47<table class="calendar"> 48 <tr> 49 <th class="w0"></th> 50 <th class="w1"></th> 51 <th class="w2"></th> 52 <th class="w3"></th> 53 <th class="w4"></th> 54 <th class="w5"></th> 55 <th class="w6"></th> 56 </tr> 57 <tr> 58 <?php 59 $dayArr = array('日', '月', '火', '水', '木', '金', '土'); 60 $index = 0; 61 for ( $i = 1; $i <= $calendar_t; $i++ ): 62 $calendar_day = date_i18n('w', strtotime($calendar_ym . '-' . $i)); 63 $calendar_date = ( $i < 10 ) ? '0' . $i : $i; 64 65 // 1日が日曜日ではない場合の空白セル 66 if ( $i == 1 && $calendar_day != 0 ): 67for ( $index = 0; $index < $calendar_day; $index++ ): 68?> 69 <td class="<?php echo 'w' . $index ?>"> </td> 70 <?php 71endfor; 72 endif; 73 74 // 祝日かどうか 75 $hol = ( in_array( $calendar_ym . '-' . $calendar_date, $holidays ) ) ? ' hol' : ''; 76 77 // 日付表示 ?> 78 <td class="<?php echo 'w' . $calendar_day .$hol ?>"><span class="date"><?php echo $i ?></span> 79 <? 80// 該当日付のイベントがあればリンクを表示 81if ( isset($events[$calendar_ym . '-' . $calendar_date]) && count( $events[$calendar_ym . '-' . $calendar_date] ) > 0 ) { 82 foreach ( $events[$calendar_ym . '-' . $calendar_date] as $event ) { 83 echo $event; 84 } 85} ?> 86 </td> 87 <?php 88 89 // 土曜日なら行末 90 if ( $calendar_day == 6 ): ?> 91 </tr> 92 <tr> 93 <?php 94 endif; 95 96 $index++; 97 98 // 最終日の後の空白 99 if ( $i == $calendar_t && $index < 42 ): 100for ( $index; $index < 42; $index++ ): 101 if ( $calendar_day == 6 ) { 102 $calendar_day = 0; ?> 103 </tr> 104 <tr> 105 <?php 106 } elseif ( $calendar_day < 6 ) { 107 $calendar_day++; 108 } ?> 109 <td class="<?php echo 'w' . $calendar_day ?>"> </td> 110 <?php 111endfor; 112 endif; 113 endfor; 114 ?> 115 </tr> 116</table> 117<?php 118} 119add_shortcode('my-calendar', 'shortcode_my_calendar'); 120

#確認したこと・試した事

var_dump($holidays);

にて、祝日が表示されることは確認しました。

set_transient(

1

の箇所あたりが間違っているのかと思うのですが、どなたかご教授いただけますでしょうか。
どうぞよろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2021/02/21 03:44

$holidays = get_holidays ($calendar_ym, $calendar_t); この$holidaysの中身はどうなってんの?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問