Get Pages to Show Up in Category & Tag Archives

In a previous post we shared how to Add Categories & Tags to WordPress Pages. But what good is that if WordPress doesn’t include pages into the category and tag archive pages. Depending on your specific implementation, not very much.

Here is a quick bit of code that we found at bajada.net that will add those pages that you have categorized or tagged into the appropriate archives.


add_filter( 'pre_get_posts' , 'ucc_include_custom_post_types' );
function ucc_include_custom_post_types( $query ) {
    global $wp_query;

    /* Don't break admin or preview pages. */
    if ( !is_preview() && !is_admin() && !is_page() && !is_single() && !is_home() ) {
        $args = array(
            'public' => true ,
            '_builtin' =>; false
        );
        $output = 'names';
        $operator = 'and';

        $post_types = get_post_types( $args , $output , $operator );
        $post_types = array_merge( $post_types , array( 'post', 'page' ) );

        if ($query->is_feed) {
            // Do feed processing here.
        } else {
            $my_post_type = get_query_var( 'post_type' );
            if ( empty( $my_post_type ) )
                $query->set( 'post_type' , $post_types );
        }
    }

    return $query;

}

Line 6 is kind of important. Here you can use Conditional Tags to prevent pages from being added under those conditions. For instance, we’ve added !is_home() because although we may want pages in our archive pages, we didn’t want them in our main blog index. Have fun.

Add Excerpt Capability to WordPress Pages

So you want to add an excerpt box to your WordPress pages that uses the built-in the_excerpt() function that’s available for posts. WordPress doesn’t see this as proper standard practice and therefore doesn’t include this ability out of the box. And they’re right. Pages and posts are different but if you came up with a way to do something that requires this ability you don’t care about that difference or “best practices”.

Fortunately, adding this to your particular WordPress theme is very easy. Simply copy and paste this code into your functions.php file in your theme directory and… voila!


add_action( 'admin_menu', 'wpnj_page_excerpt_meta_box' );
function wpnj_page_excerpt_meta_box() {
    add_meta_box( 'postexcerpt', __('Excerpt'), 'post_excerpt_meta_box', 'page', 'normal', 'core' );
}