前提・実現したいこと
① Wordpressカレンダーの日付枠内に、リンク付きで記事のタイトルを表示したい。
② リンク先を通常の日付別アーカイブページではなく、個々の投稿ページに変更したい。
③ blogというスラッグのカテゴリーだけ除外し、、カレンダーに表示させたくない。
※プラグインは使いたくない。(いろいろ試しましたが、別の要件が合わないため)
発生している問題・エラーメッセージ
①と② 通常のカレンダーしか表示できない。
③ こちらについては、欲を言えば追加で知りたい内容です。
該当のソースコード
php
1function my_get_calendar(){ 2 ob_start(); 3 get_calendar(); 4 $output = ob_get_contents(); 5 ob_end_clean(); 6 if(preg_match_all('@<td( id="today")?><a href="' . get_option('home') . '/(?m=)?([^"]*?)" title="[^"]*?">([0-9]+?)</a>@i',$output, $matches)){ 7 foreach($matches[0] as $key => $match){ 8 $date = preg_replace('@/@', '', $matches[3][$key]); 9 $year = substr($date, 0, 4); 10 $month = substr($date, 4, 2); 11 $day = substr($date, 6, 2); 12 13 global $wpdb; 14 $posts = $wpdb->get_results("SELECT ID, post_title " 15 . "FROM $wpdb->posts " 16 . "WHERE YEAR(post_date) = '$year' " 17 . "AND MONTH(post_date) = '$month' " 18 . "AND DAY(post_date) = '$day' " 19 . "AND post_type = 'post' AND post_status = 'publish' " 20 . "ORDER BY post_date DESC" 21 ); 22 23 $link = ''; 24 foreach($posts as $post) 25 $link .= '<div><a href="'. get_permalink($post->ID) . '">' . $post->post_title . '</a></div>'; 26 27 $output = preg_replace('@' . preg_quote($match) . '@', $match . $link, $output); 28 } 29 } 30 return $output; 31}
※転載元 KOTORI blog様
https://kotori-blog.com/wordpress/calendar_title/
試したこと
https://kotori-blog.com/wordpress/calendar_title/
とその参考サイト様である
http://wpxtreme.jp/event-calendar-with-wordpress
で紹介されているコードをそれぞれ変更を加えずに実施しました。
具体的には、function.php への追加し、
front-page.phpのループ内に、<?php echo my_get_calendar(); ?>
を追加しました。
しかしながら、通常のカレンダーが表示されてしまいます。
宜しくお願いします。
補足情報(FW/ツールのバージョンなど)
PHP7.2
WordPress 5.4
です。
あなたの回答
tips
プレビュー