Last active
March 3, 2017 05:06
-
-
Save azizultex/77eee67247ae65a983c9a07e05d9928d to your computer and use it in GitHub Desktop.
Get Categories by Tag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
WEBSITE : http://www.oldstructures.com/tag/projects/ | |
*/ | |
/* get categories related to tags */ | |
function categoriesbytags_func() { | |
$tag = get_queried_object(); | |
$args = array( | |
'tag' => $tag->slug, | |
'posts_per_page' => -1 | |
); | |
$posts = query_posts($args); | |
$ids = wp_list_pluck($posts, 'ID'); | |
$output = ''; | |
$cat_ids = array(); | |
foreach ($ids as $id) { | |
$post_categories = wp_get_post_categories( $id ); | |
foreach($post_categories as $c){ | |
$cat = get_category( $c ); | |
$cat_ids[] = $cat->term_id; | |
} | |
} | |
$unique_ids = array_unique($cat_ids); | |
foreach ($unique_ids as $uid ) { | |
$cat = get_category( $uid ); | |
$output .= '<ul>'; | |
$output .= '<li><a href="'.get_category_link($cat->term_id).'">'.$cat->name.'</a> ('.$cat->category_count.')</li>'; | |
$output .= '</ul>'; | |
} | |
return $output; | |
} | |
add_shortcode('categoriesbytags', 'categoriesbytags_func'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
follow https://gist.github.com/azizultex/64fc4ea49febecc74897acb76679ca4c to create customized search form and category widgets