Skip to content

Instantly share code, notes, and snippets.

@tenman
Last active August 29, 2015 14:01
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 tenman/1c6dc55567bf09bd1f8f to your computer and use it in GitHub Desktop.
Save tenman/1c6dc55567bf09bd1f8f to your computer and use it in GitHub Desktop.
show all category posts
function raindrops_category_id2name( $str ) {
$id = ( int ) $str;
return get_cat_name( $id );
}
function raindrops_reset_val( $str ) {
return 0;
}
function compare_capital_lower_not_distinguish( $a, $b ) {
return strcasecmp( $a, $b );
}
function raindrops_get_post_array_group_by_category( $limit_posts = 5, $args = array() ) {
global $post;
$category_ids = array_map( 'raindrops_category_id2name', get_all_category_ids() );
$category_ids = array_flip( $category_ids );
$category_ids = array_map( 'raindrops_reset_val', $category_ids );
if ( empty( $args ) ) {
query_posts( array( 'posts_per_page' => -1, 'post__status' => 'publish' ) );
} else {
query_posts( $args );
}
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
$categories = get_the_category();
//var_dump( $categories );
foreach ( $categories as $key => $val ) {
if ( empty( $result[$val -> name] ) || count( $result[$val -> name] ) < $limit_posts ) {
$result[$val -> name][$post -> ID] = $post -> ID;
}
}
}
}
wp_reset_query();
uksort( $result, "compare_capital_lower_not_distinguish" );
return apply_filters( 'raindrops_get_post_array_group_by_category', $result );
}
function raindrops_display_recent_post_group_by_category( $limit_posts = 5, $args = array() ) {
$raindrops_get_post_array_group_by_category = raindrops_get_post_array_group_by_category( $limit_posts, $args );
$raindrops_date_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' );
// $time_elements = raindrops_doctype_elements( 'abbr', 'time', false );
$result = apply_filters( 'raindrops_display_recent_post_group_by_category_before', '' );
$wrap_html = '<ul class="xoxo">%1$s</ul>';
$category_title = '<li><h3 class="category-title"><a href="%1$s">%2$s</a></h3><ul>';
$entry_item = '<li><a href="%1$s">%3$s</a><p><span title="%4$s">%2$s</span> </p>';
$loop_end_html = '</li></ul></li>';
foreach ( $raindrops_get_post_array_group_by_category as $key => $vals ) {
$cat_id = get_cat_ID( $key );
if ( !empty( $vals ) ) {
$result .= sprintf( $category_title, get_category_link( $cat_id ), $key );
}
foreach ( $vals as $val ) {
$permalink = esc_url( get_permalink( $val ) );
$date = get_the_time( $raindrops_date_format, $val );
$title = get_the_title( $val );
$result .= sprintf( $entry_item, $permalink, $date, $title, $date );
}
$result .= $loop_end_html;
}
$result = sprintf( $wrap_html, $result );
return apply_filters( 'raindrops_display_recent_post_group_by_category', $result );
}
@tenman
Copy link
Author

tenman commented May 21, 2014

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