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

WordPress Changing Pluggable Functions Without Child Theme

A new concept (?) – to change a pluggable function of a WordPress theme without creating a child theme.

Example – to remove the  post tags output from the posts of Twenty Twelve.

Often, these things can be done by editing the styles of the theme, however in this case, CSS would not work as the tags don’t have their own CSS class.

Creating a child theme is usually recommended as the easiest option; but this might be over the top if the only changed paremeters are from some pluggable functions.

The alternative way would be to create a plugin to alter (in this example) the post-meta-data output, which is done through the function ‘twentytwelve_entry_meta()’.

How to:
use FTP to create a file, for example with the file name twentytwelve-entry-meta-modified.php, in the /wp-content/plugins/ folder; and add this example code (http://pastebin.com/7dMVvC9E)

<?php
/*
Plugin Name: Entry Meta Twenty Twelve Modified
Plugin URI: n/a
Description: personalization of the entry meta of Twenty Twelve without child theme; in this particular example the removal of the tags.
Version: 1.0
Author: alchymyth
Author URI: http://transformationpowertools.com
License: GPL2
*/

add_action( 'setup_theme', 'modify_pluggable_theme_functions' );

function modify_pluggable_theme_functions() {

if( wp_get_theme() == 'Twenty Twelve' && get_stylesheet_directory() == get_template_directory() ) :
//check for the right theme and that no child theme is used
function twentytwelve_entry_meta() {
// Translators: used between list items, there is a space after the comma.
$categories_list = get_the_category_list( __( ', ', 'twentytwelve' ) );

$date = sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><time datetime="%3$s">%4$s</time></a>',
esc_url( get_permalink() ),
esc_attr( get_the_time() ),
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() )
);

$author = sprintf( '<span><a href="%1$s" title="%2$s" rel="author">%3$s</a></span>',
esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
esc_attr( sprintf( __( 'View all posts by %s', 'twentytwelve' ), get_the_author() ) ),
get_the_author()
);

if ( $categories_list ) {
$utility_text = 'This entry was posted in %1$s on %2$s<span> by %3$s</span>.';
} else {
$utility_text = 'This entry was posted on %2$s<span> by %3$s</span>.';
}

printf(
$utility_text,
$categories_list,
$date,
$author
);
}
endif;
}
?>

The code checks that Twenty Twelve is the active theme, and that no child theme is activated.

Activate the plugin ‘Entry Meta Twenty Twelve Modified’ as usual from within dashboard – plugins.

http://codex.wordpress.org/Pluggable_Functions
http://codex.wordpress.org/FTP_Clients
http://codex.wordpress.org/Plugins
http://codex.wordpress.org/Writing_a_Plugin
http://codex.wordpress.org/Plugin_API/Action_Reference

Linking or Enqueuing Stylesheet in WordPress Themes

There are several ways of adding a stylesheet to a WordPress theme;
examples:

A: linking the stylesheet (in the head section of header.php)

– linking the main stylesheet style.css:

<link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo( 'stylesheet_url' ); ?>" />

or

<link rel="stylesheet" type="text/css" media="all" href="<?php echo get_stylesheet_directory(); ?>" />

– linking another stylesheet custom.css:

<link rel="stylesheet" type="text/css" media="all" href="<?php echo get_stylesheet_directory_uri(); ?>/custom.css" />

B: enqueuing the stylesheet (in functions.php)

– enqueuing the main stylesheet style.css of a theme:

function theme_styles() {
	wp_enqueue_style( 'theme-style', get_stylesheet_uri() );
}
add_action( 'wp_enqueue_scripts', 'theme_styles' );

– enqueuing another stylesheet custom.css of a child theme:

function child_theme_styles() {
	wp_enqueue_style( 'custom-style', get_stylesheet_directory_uri() . '/custom.css' );
}
add_action( 'wp_enqueue_scripts', 'child_theme_styles', 20 );

C: using @import in an existing stylesheet

– example linking the parent theme’s style.css in a child theme’s style.css

@import url('../parentheme/style.css');

Resources:

WordPress Theme Development – style.css

link html
enqueue stylesheet
@import

Integrate Pagination Plugins into Twenty Twelve

The ‘next/prev’ posts links are done with a function in Twenty Twelve; twentytwelve_content_nav().

That function is pluggable, which makes it easy to change it in a child theme of Twenty Twelve.
To integrate for instance the plugin ‘wp_pagenavi’, you would need to change the function as follows (when you are using a child theme, add this into functions.php of the child theme; else edit the function in Twenty Twelve):

function twentytwelve_content_nav( $html_id ) {
	global $wp_query;

	$html_id = esc_attr( $html_id );

	if ( $wp_query->max_num_pages > 1 ) : ?>
		<nav id="<?php echo $html_id; ?>" class="navigation" role="navigation">
			<h3 class="assistive-text"><?php _e( 'Post navigation', 'twentytwelve' ); ?></h3>
		<?php if( function_exists( 'wp_pagenavi' ) ) { wp_pagenavi(); } else { ?>	
			<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'twentytwelve' ) ); ?></div>
			<div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'twentytwelve' ) ); ?></div>
		<?php } ?>
		</nav><!-- #<?php echo $html_id; ?> .navigation -->
	<?php endif;
}

the use of the conditional check if( function_exists( 'wp_pagenavi' ) ) prevents an error message if the plugin is not activated, and provides the theme’s default pagination as a fallback.