teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

回答を修正し、ソースコード記載

2017/02/08 13:14

投稿

退会済みユーザー
answer CHANGED
@@ -12,4 +12,84 @@
12
12
  ###追記
13
13
  以前も似たような質問を見て、回答した気がするなあと探してみたらありました。
14
14
  - PHP - wordpress カスタム投稿&カテゴリー毎の月次アーカイブの作り方(59475)|teratail
15
- [https://teratail.com/questions/59475](https://teratail.com/questions/59475)
15
+ [https://teratail.com/questions/59475](https://teratail.com/questions/59475)
16
+
17
+ ###追記2
18
+ 前にあげた回答がよくなかったので、書き直しました。
19
+ 以下で試してみてください。
20
+ `?><pre><?php`から`?></pre><?php`の部分はデバッグ用なので最終的には削除してください。
21
+
22
+ ####taxonomy-specialtopics_cat.phpに記述
23
+ ```PHP
24
+ <select name="archive-dropdown" onChange='document.location.href=this.options[this.selectedIndex].value;'>
25
+ <option value=""><?php echo attribute_escape(('年度別')); ?></option>
26
+ <?php wp_get_archives('type=yearly&post_type=specialtopics&format=option&show_post_count=1'); ?>
27
+ </select>
28
+ ```
29
+
30
+ ####functions.php
31
+ ```PHP
32
+ function my_getarchives_where( $where, $args ){
33
+ $taxonomy = get_query_var('taxonomy');
34
+ $term = get_query_var('term');
35
+
36
+ ?><pre><?php
37
+ var_dump($taxonomy);
38
+ var_dump($term);
39
+ ?></pre><?php
40
+
41
+ if(!empty($taxonomy)){
42
+ $where = $where . " AND t.taxonomy = '" . $taxonomy . "'";
43
+ }
44
+ if(!empty($term)){
45
+ $where = $where . " AND terms.slug = '" . $term . "'";
46
+ }
47
+
48
+ ?><pre><?php
49
+ var_dump($where);
50
+ ?></pre><?php
51
+
52
+ return $where;
53
+ }
54
+ add_filter( 'getarchives_where', 'my_getarchives_where', 10, 2 );
55
+
56
+ function my_getarchives_join($join, $args){
57
+ global $wpdb;
58
+
59
+ $join = " LEFT JOIN $wpdb->term_relationships as r ON $wpdb->posts.ID = r.object_ID
60
+ LEFT JOIN $wpdb->term_taxonomy as t ON r.term_taxonomy_id = t.term_taxonomy_id
61
+ LEFT JOIN $wpdb->terms as terms ON t.term_id = terms.term_id";
62
+
63
+ ?><pre><?php
64
+ var_dump($join);
65
+ ?></pre><?php
66
+
67
+ return $join;
68
+ }
69
+ add_filter('getarchives_join', 'my_getarchives_join', 10, 2);
70
+
71
+ function my_year_link($url, $year){
72
+
73
+ $taxonomy = get_query_var('taxonomy');
74
+ $term = get_query_var('term');
75
+
76
+ ?><pre><?php
77
+ var_dump($taxonomy);
78
+ var_dump($term);
79
+ ?></pre><?php
80
+
81
+ if( !empty($taxonomy) ) {
82
+ $url = add_query_arg( 'taxonomy', $taxonomy, $url);
83
+ }
84
+ if( !empty($term) ) {
85
+ $url = add_query_arg( 'term', $term, $url);
86
+ }
87
+
88
+ ?><pre><?php
89
+ var_dump($url);
90
+ ?></pre><?php
91
+
92
+ return $url;
93
+ }
94
+ add_filter('year_link', 'my_year_link', 10, 2);
95
+ ```

1

類似質問へのURL追記

2017/02/08 13:14

投稿

退会済みユーザー
answer CHANGED
@@ -7,4 +7,9 @@
7
7
 
8
8
  **参考URL**
9
9
  - テンプレートタグ/wp get archives - WordPress Codex 日本語版
10
- [http://wpdocs.osdn.jp/%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88%E3%82%BF%E3%82%B0/wp_get_archives](http://wpdocs.osdn.jp/%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88%E3%82%BF%E3%82%B0/wp_get_archives)
10
+ [http://wpdocs.osdn.jp/%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88%E3%82%BF%E3%82%B0/wp_get_archives](http://wpdocs.osdn.jp/%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88%E3%82%BF%E3%82%B0/wp_get_archives)
11
+
12
+ ###追記
13
+ 以前も似たような質問を見て、回答した気がするなあと探してみたらありました。
14
+ - PHP - wordpress カスタム投稿&カテゴリー毎の月次アーカイブの作り方(59475)|teratail
15
+ [https://teratail.com/questions/59475](https://teratail.com/questions/59475)