Skip to content

Instantly share code, notes, and snippets.

@azizultex
Last active March 3, 2017 05:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azizultex/77eee67247ae65a983c9a07e05d9928d to your computer and use it in GitHub Desktop.
Save azizultex/77eee67247ae65a983c9a07e05d9928d to your computer and use it in GitHub Desktop.
Get Categories by Tag
/*
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');
@azizultex
Copy link
Author

follow https://gist.github.com/azizultex/64fc4ea49febecc74897acb76679ca4c to create customized search form and category widgets

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment