How to display categories in Automattic’s P2 Theme

- by

By default, Automattic’s phenomenal P2 theme does not support posting into categories from the front page, it only supports tags. My fork of the theme called P2 Categories does that though and lets you conveniently select a category from a drop down menu right there on the front page.

I wrote an update to it last week, and in so doing my article from 2013 came in handy that explains how to add this functionality to P2. Nothing much has changed in the source code, so it’s still relevant and accurate.

What the above article did not explain however was how to show which category a post belongs to. And because it’s still fresh in my  memory how to do this, I thought I’d better write it down for next time (and anyone who’s interested in how to do it).

Up until P2 Categories version 1.5 I added a bodge in the /entry.php file around line 75. It would simply extract the categories from the current post and add them inside the meta data block, right underneath the Tags.

There was an issue with this approach however: because if a post was in two categories or more, only the last category’s post count would show up correctly. Unlike with tags, whose post count shows no matter how many tags you had.

So I took another crack at it in version 1.6 and decided to split out the code needed for this into its own method – just like the P2 developers did with tags.

Displaying Categories alongside Tags

Take a look at entry.php and find a block starting and finishing with a span tag for a CSS class called “tags”. It’s in line 57:

<span class="tags">
<?php tags_with_count( '', __( '<br />Tags:' , 'p2' ) .' ', ', ', ' &nbsp;' ); ?>&nbsp;
</span>

This calls a method that prints out the amounts of tags a post belongs to. Just after the closing span tag, we’ll call a similar method of our own that will do the same for categories.

The finished version looks like this:

<span class="tags">
 <?php tags_with_count( '', __( '<br />Tags:' , 'p2' ) .' ', ', ', ' &nbsp;' ); ?>&nbsp;
 </span>
<?php endif; ?>

<?php
// display categories here
if (!is_page()) {
 if (!in_category('uncategorized')) { ?>
 <span class="categories">
 <?php 
 cats_with_count( '', __( '<br />Categories:' , 'p2' ) .' ', ', ', ' &nbsp;' ); ?>&nbsp;
 </span>

This code will check if we’re displaying a single post or a page and call our function we’ll add in the next step. If we are showing a page then we do not want to show categories (because pages do not have taxomony). The second if statement checks if our post is in “uncategorized”, in which case that category is omitted. That way only meaningful categories are shown.

Adding Functions

We need two additional functions, following the orginal P2 code for Tags, amended for categories. Here they are:

// build string for category meta
function get_cats_with_count( $post, $format = 'list', $before = '', $sep = '', $after = '' ) {

  $postcats = get_the_category($post->ID, 'category' );
  if ( !$postcats )
 return '';

  foreach ( $postcats as $cat ) {
 if ( $cat->count > 1 && !is_category($cat->slug) ) {
 $cat_link = '<a href="' . get_category_link( $cat ) . '" rel="category">' . $cat->name . ' ( ' . number_format_i18n( $cat->count ) . ' )</a>';
 } else {
 $cat_link = $cat->name;
 }

 if ( $format == 'list' )
 $cat_link = '<li>' . $cat_link . '</li>';

 $cat_links[] = $cat_link;
  }

  return apply_filters( 'cats_with_count', $before . join( $sep, $cat_links ) . $after, $post );
}

// insert category meta into post
function cats_with_count( $format = 'list', $before = '', $sep = '', $after = '' ) {
  global $post;
  echo get_cats_with_count( $post, $format, $before, $sep, $after );
}

You can add this code either to the original functions.php file, your child theme’s functions.php file, or – like I did for P2 Categories – create a separate functions file to separate your own amendments from those of the original developers.

If you want to do that, include your own file using something like this in the original functions.php file:

require_once ( get_template_directory() . '/functions-p2-categories.php');

The result looks something like this:

Full Code on GitHub

I’ve implemented these changes exactly like this in my P2 Categories theme in version 1.6. It’s a working example, feel free to take a peek, improve, share and enjoy:



If you enjoy my content, please consider supporting me on Ko-fi. In return you can browse this whole site without any pesky ads! More details here.

1 thought on “How to display categories in Automattic’s P2 Theme”

Leave a Comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.