あるカテゴリー内のタグを取得してリンクを表示する。
パーマリンクは/%category%/%post_id%.html
archive.php
<?php
$ca = get_the_category();
$ca_id = $ca[0]->cat_ID;
$ca_url = get_category_link($ca_id);
$ca_slug = $ca[0]->slug;
$ca_name = $ca[0]->cat_name;
getAllTagsInCat($ca_id, $ca_slug);
?>
<?php
function getAllTagsInCat($cat_id,$cat_slug) {
global $wpdb;
$tag_join = array();
$sql_object_id = "SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id = " . $cat_id;
$res_object_id = $wpdb->get_results($sql_object_id);
$object_id_num = count($res_object_id);
for ($i=0; $i<$object_id_num; $i++){
$sql_term_id = "SELECT term_id FROM wp_term_taxonomy LEFT JOIN wp_term_relationships ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id WHERE wp_term_relationships.object_id = " . $res_object_id[$i]->object_id . " AND wp_term_taxonomy.taxonomy = 'post_tag'";
$res_term_id = $wpdb->get_results($sql_term_id);
$term_id_num = count($res_term_id);
if($term_id_num != 0){
for ($j=0; $j<$term_id_num; $j++) {
$sql_tag = "SELECT slug, name FROM wp_terms WHERE term_id = " . $res_term_id[$j]->term_id;
$res_tag = $wpdb->get_results($sql_tag);
$tag_str = $res_tag[$j]->slug . "," . $res_tag[$j]->name;
array_push($tag_join, $tag_str);
}
}
}
$tag_join = array_unique($tag_join);
foreach ($tag_join as $value) {
$tag_explode = explode(",", $value);
if($tag_explode[0] !== ''){
$html = '<a href="/category/' . $cat_slug . '?tag=' . $tag_explode[0] . '">' . $tag_explode[1] . '</a> | ';
echo($html);
}
}
}
?>
[…] 固定ページで特定のカテゴリーに属したタグ名を表示したい ⇒ カテゴリーとタグによる絞り込み(作成中) ⇒ […]
改行と段落タグは自動で挿入されます。
メールアドレスは表示されません。