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

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

新規登録して質問してみよう
ただいま回答率
85.50%
WordPress

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

Q&A

解決済

1回答

5760閲覧

Wordpress 年月アーカイブのリンク先※カスタム投稿タイプの場合

pippi_385

総合スコア10

WordPress

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

0グッド

0クリップ

投稿2020/04/30 05:59

現在Wordpressでトピックスの一覧ページを制作中です。

WordpressやPHPはネットの情報をコピーしながらなんとか形になるかな…という程度の知識しかございません。

こちらのサイトを参考にし、
https://webspace.jp/blog/27/

ページの右側に年月ごとのアーカイブを作成し、各年月の記事を取得するところまではできました。
イメージ説明

しかし、この○月のリンクをクリックすると、リンク先が
http://○○○/○○○/date/2020/03/
となってしまいます。
これを、
http://○○○/○○○/topics/date/2020/03/
(↑現状、こっちのURLを叩くと該当の年月一覧ページが表示される)
もしくは
http://○○○/○○○/topics/2020/03/
としたいのですが、やり方がわかりません。

●カスタム投稿タイプはCPT UIを用いており、スラッグは「topics」です。

●プラグイン『Custom Post Type Date Archives』を入れており、
Add date archivesにチェックを入れています。

●パーマリンクの共通設定は、
http://○○○/○○○/%category%/%post_id%/

●カスタム投稿タイプのパーマリンク設定は
http://○○○/○○○/topics/%post_id%/
has_archive: true / with_front: true

●date-topics.phpのテンプレートを作ってサーバーアップしています。

実際のコードはこちらです。

//▼▼▼archive-topics.php <aside class="topics-SortArea"> <h4 class="archiveHead">Archive</h4> <?php dynamic_sidebar('archive');?> </aside> //▼▼▼functions.php //年月アーカイブウィジェット(年月用)組み込み class Widget_Archives2 extends WP_Widget { function __construct() { $widget_ops = array('classname' => 'widget_archives2', 'description' => 'サイトの投稿の年別/月別アーカイブ' ); parent::__construct('archives2', 'アーカイブ (年別/月別)', $widget_ops); } function widget( $args, $instance ) { extract($args); $c = ! empty( $instance['count'] ) ? '1' : '0'; $title = apply_filters('widget_title', empty($instance['title']) ? __('Archives') : $instance['title'], $instance, $this->id_base); echo $before_widget; if ( $title ) echo $before_title . $title . $after_title; $this->get_archives(apply_filters('widget_archives2_args', array('show_post_count' => $c))); echo $after_widget; } function update( $new_instance, $old_instance ) { $instance = $old_instance; $new_instance = wp_parse_args( (array) $new_instance, array( 'title' => '', 'count' => 0) ); $instance['title'] = strip_tags($new_instance['title']); $instance['count'] = $new_instance['count'] ? 1 : 0; return $instance; } function form( $instance ) { $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'count' => 0) ); $title = strip_tags($instance['title']); $count = $instance['count'] ? 'checked="checked"' : ''; ?> <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p> <p> <input class="checkbox" type="checkbox" <?php echo $count; ?> id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" /> <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Show post counts'); ?></label> </p> <?php } function get_archives($args = '') { $defaults = array( 'limit' => '', 'before' => '', 'after' => '', 'show_post_count' => false, 'echo' => 1, 'order' => 'DESC', ); $r = wp_parse_args( $args, $defaults ); extract( $r, EXTR_SKIP ); $arcresults = $this->get_monthly_archives_data($r); $output = $this->build_html($r, $arcresults); if ( $echo ) echo $output; else return $output; } function get_monthly_archives_data($args) { global $wpdb; extract( $args, EXTR_SKIP ); if ( '' != $limit ) { $limit = absint($limit); $limit = ' LIMIT '.$limit; } $order = strtoupper( $order ); if ( $order !== 'ASC' ) $order = 'DESC'; //filters $where = apply_filters( 'getarchives2_where', "WHERE post_type = 'topics' AND post_status = 'publish'", $args ); $join = apply_filters( 'getarchives2_join', '', $args ); $query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date $order $limit"; $key = md5($query); $cache = wp_cache_get( 'get_archives2' , 'general'); if ( !isset( $cache[ $key ] ) ) { $arcresults = $wpdb->get_results($query); $cache[ $key ] = $arcresults; wp_cache_set( 'get_archives2', $cache, 'general' ); } else { $arcresults = $cache[ $key ]; } return $arcresults; } function build_html($args, $arcresults) { extract( $args, EXTR_SKIP ); if ( !$arcresults ) return ''; $cur_year = -1; $afterafter = $after; $output = '<ul>'; // (1) foreach ( (array) $arcresults as $arcresult ) { if ( $cur_year != $arcresult->year ) { if ( $cur_year > 0 ) { $output .= "</ul>"; // (/3) $output .= "</li>\n"; // (/2) } $output .= "<li><span>" . $arcresult->year . "年</span>"; // (2) $output .= "<ul>\n"; // (3) $cur_year = $arcresult->year; } if ( $show_post_count ) $after = "&nbsp;({$arcresult->posts}){$afterafter}"; $output .= "\t<li>" . $this->get_archives_link($arcresult->year, $arcresult->month, $before, $after) . "</li>\n"; } $output .= "</ul>"; // (/3) $output .= "</li>\n"; // (/2) $output .= "</ul>\n"; // (/1) return $output; } function get_archives_link($year, $month, $before = '', $after = '') { global $wp_locale; $url = get_month_link($year, $month); $url = esc_url($url); $text = $wp_locale->get_month($month); $text = wptexturize($text); $title_text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($month), $year); $title_text = esc_attr($title_text); $link_html = "$before<a href='$url' title='$title_text'>$text</a>$after"; $link_html = apply_filters( 'get_archives2_link', $link_html ); return $link_html; } }

いろいろと勉強不足で不十分な点もあるかと思いますが、どなたかご教授いただければ幸いです。

よろしくお願いいたします。

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

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

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

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

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

guest

回答1

0

自己解決

http://○○○/○○○/topics/date/2020/03/

こちらのURLにする方法は、
preg_replaceで解決できました。

$link_html = preg_replace('/date/','topics/date', $link_html);
こちら追加いたしました。

▼functions.php

PHP

1function get_archives_link($year, $month, $before = '', $after = '') { 2 global $wp_locale; 3 4 $url = get_month_link($year, $month); 5 $url = esc_url($url); 6 7 $text = $wp_locale->get_month($month); 8 $text = wptexturize($text); 9 10 $title_text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($month), $year); 11 $title_text = esc_attr($title_text); 12 13 $link_html = "$before<a href='$url' title='$title_text'>$text</a>$after"; 14 $link_html = apply_filters( 'get_archives2_link', $link_html ); 15 $link_html = preg_replace('/date/','topics/date', $link_html); 16 return $link_html; 17 }

投稿2020/04/30 07:16

編集2020/04/30 07:18
pippi_385

総合スコア10

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問