Alphabetize Category Posts Using ‘pre_get_posts’ Action

Occasionally you want to have a different sort order in some of the category archives, for example, you want to output the posts of a specific category in alphabetical order.
Instead of having to create a category template for each of those categories and to add a custom query to change the query parameters, WordPress providdes the ‘pre_get_posts’ action where you can add a corresponding function:

function category_archive_sort_alphabetical( $query ) {
    if ( is_admin() || ! $query->is_main_query() )
        return;

    if ( is_category( array( 'Category One', 'category-two', 'Another Cat' ) ) ) {
		$query->set( 'orderby', 'title' );
		$query->set( 'order', 'ASC' );
        return;
    }
}
add_action( 'pre_get_posts', 'category_archive_sort_alphabetical', 1 );

resources:
http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
http://codex.wordpress.org/Function_Reference/is_category

Posts in Two Or Three Columns – Twenty Twelve

If your index and archive pages are getting too long, or if you want to compact the information within those pages a little bit, for instance because the featured images above each post are just too large, consider to change the template to show the posts in two columns.

This is easier done than said, with some useful css classes injected into the output of the ‘post_class()’ in the div of each post, together with some rather simple css.

For instance, to change the category archive into two columns, without editing category.php itself, add a filter function into functions.php (as always with the default themes of WordPress, this  is done within a child theme):

add_filter('post_class','category_two_column_classes');

function category_two_column_classes( $classes ) {
global $wp_query;
if( is_category() ) :
$classes[] = 'two-column-post';
if( $wp_query->current_post%2 == 0 ) $classes[] = 'two-column-post-left';
endif;
return $classes;
}

The minimum css for this is:

.two-column-post { width: 47%; float: left; margin-left: 5.9%; }
.two-column-post-left { clear: left; margin-left: 0; }

If you want to apply the same principle for instance to the posts page or other archives, replace

is_category()

with another conditional tag like

is_home()

.
The code is easily adapted to three columns (or actually any number of columns) by changing the number in the modulus operator:

add_filter('post_class','category_three_column_classes');

function category_three_column_classes( $classes ) {
global $wp_query;
if( is_category() ) :
$classes[] = 'three-column-post';
if( $wp_query->current_post%3 == 0 ) $classes[] = 'column-post-left';
endif;
return $classes;
}

the css for three columns would be:

.three-column-post { width: 30%; float: left; margin-left: 4.9%; }
.column-post-left { clear: left; margin-left: 0; }

Editing Widget Parameters per Filter Function WordPress

If you want to change the parameters of the default widgets, for instance the font sizes of the tag cloud widget, you will notice that you can’t access the codes directly.

Editing the core files in the /wp-includes folder is obviously not an option.

Luckily, most of the default widgets are now applying filters to the parameters;
for example, in this the line the filter gets applied for the  tag cloud widget – in /wp-includes/default-widgets.php:

wp_tag_cloud( apply_filters('widget_tag_cloud_args', array('taxonomy' => $current_taxonomy) ) );

Add a filter function to functions.php of your theme;
for example changing the font size range for the tags:

(corrected 05 Feb 2013)

add_filter('widget_tag_cloud_args','set_tag_cloud_sizes');
function set_tag_cloud_sizes($args) {
$args = array_merge( $args, array('smallest' => 12, 'largest' => 19) );
return $args; }

Alternative code:

add_filter('widget_tag_cloud_args','set_tag_cloud_sizes');
function set_tag_cloud_sizes($args) {
$args['smallest'] = 12;
$args['largest'] = 19;
return $args; }

Another example – to show only tags from the current single post:

add_filter('widget_tag_cloud_args','single_post_tag_cloud_tags');
function single_post_tag_cloud_tags($args) {
if( is_single() && $args['taxonomy'] == 'post_tag' ) {
global $post;
$post_tag_ids = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );
$args = array_merge( $args, array('include' => implode(',',$post_tag_ids)) );
}
return $args;
}

See the codex chapter of wp_tag_cloud()   for all possible parameters.

Another similar example – excluding the default category ‘uncategorized’ from the category widget:

add_filter('widget_categories_args','exclude_default_cat');
function exclude_default_cat($args) {
$args['exclude'] = '1';
return $args;
}

A further example – removing the output of the category description as title attributes in the category widget:

add_filter('widget_categories_args','remove_cat_description_widget');
function remove_cat_description_widget($args) {
$args['use_desc_for_title'] = 0;
return $args;
}

Here is the list of all filters for the default widgets:

'widget_pages_args'
'widget_links_args'
'widget_archives_dropdown_args'
'widget_archives_args'
'widget_text'
'widget_categories_dropdown_args'
'widget_categories_args'
'widget_tag_cloud_args'

Category Archive Navigation

A simple next/previous category navigation for the category archive; particular useful if you use your categories to organise your blog, for instance your products, or photos…

<?php
foreach(get_categories() as $all_cat) {  $cat_ids[] = $all_cat->term_id; }
 $this_cat = get_query_var('cat');
 $this_cat_position = array_search( $this_cat, $cat_ids ); ?>
<div>
<div>
<?php $prev_cat_position = $this_cat_position -1;
 if( $prev_cat_position >=0 ) {
 $prev_cat_id = array_slice( $cat_ids, $prev_cat_position, 1 );
 echo '<a href="' . get_category_link($prev_cat_id[0]) . '">&laquo; ' . get_category($prev_cat_id[0])->name . '</a>'; } ?>
</div>
<div>
<?php $next_cat_position = $this_cat_position +1;
 if( $next_cat_position < count($cat_ids) ) {
 $next_cat_id = array_slice( $cat_ids, $next_cat_position, 1 );
echo '<a href="' . get_category_link($next_cat_id[0]) . '">' . get_category($next_cat_id[0])->name . ' &raquo;</a>'; } ?>
</div>
</div>

the above has a commonly used html structure to help with formatting.