Skip to content

Instantly share code, notes, and snippets.

@eri-trabiccolo
Created March 12, 2015 12:18
Show Gist options
  • Save eri-trabiccolo/6fb8671b5db73bc72dab to your computer and use it in GitHub Desktop.
Save eri-trabiccolo/6fb8671b5db73bc72dab to your computer and use it in GitHub Desktop.
Reverse post order in a specific category
// Runs before the posts are fetched
add_action( 'pre_get_posts' , 'specific_category_reverse_post_order' );
function specific_category_reverse_post_order( $query ) {
// Alter only the primary query and only if we are in post lists context
// How to use is_category -> http://codex.wordpress.org/Function_Reference/is_category
if( ! ( $query->is_main_query() && is_category('3') ) )
return;
$query->set( 'order' , 'ASC' );
// reverse the pagination links
add_filter( 'tc_list_nav_previous_text', 'previous_text' );
add_filter( 'tc_list_nav_next_text', 'previous_text' );
// add css to swap the pagination buttons
add_filter( 'tc_user_options_style', 'sc_reverse_post_order_inline_css' );
}
function previous_text(){
$current_filter = current_filter();
$filter_labels = array(
//filter => label
'tc_list_nav_previous_text' => __( '<span class="meta-nav">&larr;</span> Older posts' , 'customizr-child' ),
'tc_list_nav_next_text' => __( 'Newer posts <span class="meta-nav">&rarr;</span>' , 'customizr-child' )
);
return $filter_labels[$current_filter];
}
function sc_reverse_post_order_inline_css( $_css ){
return sprintf("%s\n%s",
$_css,
'
#nav-below .nav-previous { float: right;}
#nav-below .nav-next { float: left;}
'
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment