Instantly share code, notes, and snippets.
Last active
August 29, 2015 14:03
-
Save Chrico/ccfab979eae532768322 to your computer and use it in GitHub Desktop.
WordPress Dropin: Getting popular Tags by Category
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
<?php | |
/** | |
* Fetching popular Tags from a category | |
* | |
* @param Array $args | |
* @return String $output | |
*/ | |
function chrico_get_popular_tags_by_category( Array $args = array() ){ | |
// our default args to generate the output | |
$default_args = array( | |
'max_items' => 5, | |
'link' => '<a href="%1$s">%2$s (%3$s)</a>', | |
'link_separator'=> ' | ', | |
'link_before' => '', | |
'link_after' => '', | |
'before' => __( '<h3 class="pota">Popular tags in this category:</h3><p> ', 'chrico' ), | |
'after' => '</p>', | |
); | |
// getting the current category as default, if exists | |
if( is_category() ) { | |
$query_var = get_query_var( 'cat' ); | |
$category = get_category( $query_var ); | |
if( !is_wp_error( $category ) ) { | |
$default_args[ 'category_id' ] = $category->cat_ID; | |
} | |
} | |
$args = wp_parse_args( $args, $default_args ); | |
/** | |
* arg filter to manipulate the $args | |
* @param Array $args | |
* @param Array $default_args | |
*/ | |
$args = apply_filters( 'chrico_get_popular_tags_by_category_args', $args, $default_args ); | |
if( !isset( $args[ 'category_id' ] ) || empty( $args[ 'category_id' ] ) ) { | |
return ''; | |
} | |
// fetching all posts in this category | |
$post_args = array( | |
'category' => $args[ 'category_id' ], | |
'fields' => 'ids', | |
'numberposts' => -1 | |
); | |
$posts = get_posts( $post_args ); | |
if( empty( $posts ) ) { | |
return ''; | |
} | |
// searching for tags and sorting them. | |
$tag_order = array(); | |
$tags = array(); | |
$post_tag_args = array( | |
'orderby' => 'count', | |
'order' => 'DESC' | |
); | |
foreach( $posts as $post_id ) { | |
// getting all post_tags | |
$post_tags = wp_get_post_tags( $post_id, $post_tag_args ); | |
// looping through them and adding the count | |
foreach( $post_tags as $tag ) { | |
if( !array_key_exists( $tag->slug, $tags ) ) { | |
$tag_order[ $tag->slug ]= 0; | |
$tags[ $tag->slug ] = $tag; | |
} | |
$tag_order[ $tag->slug ] += 1; | |
} | |
} | |
// no tags to order? break. | |
if( count( $tag_order ) < 1 ) { | |
return ''; | |
} | |
// sorting the tags from high to low | |
arsort ( $tag_order ); | |
// we don't need all items, do we? | |
if( $args[ 'max_items' ] !== -1 ) { | |
$tag_order = array_slice( | |
$tag_order, | |
0, | |
$args[ 'max_items' ] | |
); | |
} | |
// generating the output~ | |
$output = ''; | |
$counter = 1; | |
$count_tags = count( $tag_order ); | |
foreach( $tag_order as $tag_slug => $count ) { | |
$tag = $tags[ $tag_slug ]; | |
$term_link = get_term_link( $tag->slug, 'post_tag' ); | |
// building the link | |
$link = sprintf( | |
$args[ 'link' ], | |
$term_link, | |
$tag->name, | |
$count | |
); | |
$output .= $args[ 'link_before' ]; | |
$output .= $link; | |
if( $counter < $count_tags ) { | |
$output .= $args[ 'link_separator' ]; | |
} | |
$output .= $args[ 'link_after' ]; | |
$counter++; | |
} | |
// building the context-array for return-filter | |
$context = array( | |
'tag_order' => $tag_order, | |
'tags' => $tags, | |
'posts' => $posts, | |
'post_args' => $post_args, | |
'post_tag_args' => $post_tag_args, | |
'args' => $args, | |
'default_args' => $default_args | |
); | |
// the html-output | |
$html = $args[ 'before' ] . $output . $args[ 'after' ]; | |
/** | |
* return filter to manipulate the output | |
* @param String $html | |
* @param Array $context array( | |
'tag_order' => Array, | |
'tags' => Array, | |
'post_tag_args' => Array, | |
'posts' => Array, | |
'post_args' => Array, | |
'args' => Array, | |
'default_args => Array | |
) | |
*/ | |
return apply_filters( 'chrico_get_popular_tags_by_category', $html, $context ); | |
} |
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
<?php | |
/** | |
* get popular tags when is_category() | |
*/ | |
echo chrico_get_popular_tags_by_category(); | |
/***************************************/ | |
/** | |
* get tags by category 5 | |
*/ | |
$args = array( | |
'category_id' => 5 | |
); | |
echo chrico_get_popular_tags_by_category( $args ); | |
/***************************************/ | |
/** | |
* get more tags by category | |
*/ | |
$args = array( | |
'max_items' => 10 | |
); | |
echo chrico_get_popular_tags_by_category( $args ); | |
/***************************************/ | |
/** | |
* get ALL tags by category | |
*/ | |
$args = array( | |
'max_items' => -1 | |
); | |
echo chrico_get_popular_tags_by_category( $args ); | |
/***************************************/ | |
/** | |
* format the output as an ulli with a h4 headline before :) | |
*/ | |
$args = array( | |
'before' => '<h4>' . __( 'My custom before text', 'chrico') . '</h4><ul>', | |
'after' => '</ul>', | |
'before_link' => '<li>', | |
'after_link' => '<li>' | |
'link' => '<a href="%1$s"><span class="title">%2$s</span> <span class="count">%3$s</span></a>', | |
'link_separator'=> ' - ' | |
); | |
echo chrico_get_popular_tags_by_category( $args ); | |
/***************************************/ | |
/** | |
* same as above, but using the filter~ | |
*/ | |
add_filter( 'chrico_get_popular_tags_by_category_args', 'demo_filter_chrico_get_popular_tags_by_category_args' ); | |
/** | |
* arg-filter-callback to manipulate the $args | |
* @param Array $args | |
* @return Array $args | |
*/ | |
function demo_filter_chrico_get_popular_tags_by_category_args( Array $args ) { | |
$args[ 'before' ] = '<h4>' . __( 'My custom before text', 'chrico') . '</h4><ul>'; | |
$args[ 'after' ] = '</ul>'; | |
$args[ 'before_link' ] = '<li>'; | |
$args[ 'after_link' ] = '<li>'; | |
$args[ 'link' ] = '<a href="%1$s"><span class="title">%2$s</span> <span class="count">%3$s</span></a>'; | |
$args[ 'link_separator' ] = ' - '; | |
return $args; | |
} | |
/***************************************/ | |
/** | |
* filter the output | |
*/ | |
add_filter( 'chrico_get_popular_tags_by_category_args', 'demo_filter_chrico_get_popular_tags_by_category' ); | |
/** | |
* return-filter-callback to manipulate the output by adding a <section> around | |
* @param String $html | |
*/ | |
function demo_filter_chrico_get_popular_tags_by_category( $html ) { | |
$html = '<section>' . $html . '</section>'; | |
return $html; | |
} | |
/***************************************/ | |
/* to be continued... */ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment