wordpressでブログを運営しています。
1からコーディングしたカスタムテーマを使用中です。
ブログサイドバーに、特定カテゴリのみの月別アーカイブリストを作りました。
こちらのサイトを参考にしました。
コードは、function.phpに下記です。(上記サイト様よりコピペ)
add_filter('getarchives_where', 'custom_archives_where', 10, 2); add_filter('getarchives_join', 'custom_archives_join', 10, 2); function custom_archives_join($x, $r) { global $wpdb; $cat_ID = $r['cat']; if (isset($cat_ID)) { return $x . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)"; } else { return $x; } } function custom_archives_where($x, $r) { global $wpdb; $cat_ID = $r['cat']; if (isset($cat_ID)) { return $x . " AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id IN ($cat_ID)"; } else { $x; } } function wp_get_cat_archives($opts, $cat) { $args = wp_parse_args($opts, array('echo' => '1')); // default echo is 1. $echo = $args['echo'] != '0'; // remember the original echo flag. $args['echo'] = 0; $args['cat'] = $cat; $tag = ($args['format'] === 'option') ? 'option' : 'li'; $archives = wp_get_archives(build_query($args)); $archs = explode('</'.$tag.'>', $archives); $links = array(); foreach ($archs as $archive) { $link = preg_replace("/='([^']+)'/", "='$1?cat={$cat}'", $archive); array_push($links, $link); } $result = implode('</'.$tag.'>', $links); if ($echo) { echo $result; } else { return $result; } }
出力は
<ul> <?php wp_get_cat_archives('type=monthly&format=option&show_post_count=1', 2); ?> </ul>
ですが、これだと、アーカイブは表示できてもリンクが付きません。
追記:
日付アーカイブへのリンクを生成するには、以下のようなコードで合っているでしょうか。
合っているならば、これをfunction.phpに入れるには、上記のどの行へ挿入したらいいのでしょうか。
手探りで追加しようとしてもエラーしてしまうため、困っています。
別の方法といっても、発想力が乏しく、検索にも疲れてきました。
お知恵を拝借できますでしょうか。よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。