Simple Language-Defined posts in WordPress

For different audiences I write blog posts in different languages. Developers usually understand English and often even talk in English, it’s their lingua franca of the programming domain.
On the other hand other articles are about more local topics, so their target audience is mainly Germans. Here I prefer writing in my mother tongue. Unfortunately there seems to be no easy and simple solution to define the language per article in WordPress, so I considered to write one myself.

The plugin is available for download on github now.

Read further for more details on What it does, How it is done, Why I did it and What could be added. Simple Language-Defined posts in WordPress weiterlesen

Customized WordPress-Template for Category-Subtree

For the Advent calendar categories I wanted to have a custom template that applies to all categories below the Adventskalender category (that one included).

WordPress has a great Template system, using inheritance between templates, as shown in the Codex. Utilizing this system it is possible to customize all category pages or one category page in particular – but there seems to be no way to customize a set of categories based on the category hierarchy.

On the other hand WordPress contains a function to get a Breadcrumb trail from the root to the current category. For this function, get_category_parents() no non-grapical equivalent seems to exist unfortunately.

On the other hand the code is quite simple: Start at the category of your choice and recursively get the parent category up to the root.

My current solution is based on a fixed „root“ category, as that is what I want to have (more ideas on how the template system could be extended nicely see below).

My solution utilizes the action hook  ‚category_template‘ which is not documented unfortunately in the codex (although listed in the filter reference). The following is the code listing of the function:

function get_advent_category_template( $archive_template )
  // get the category:
  $catId = get_query_var('cat');
  $found = false;

  $yourcat = get_category($catId);
  do {
    if ($yourcat->slug == 'adventskalender')
      return dirname(__FILE__).'/category-adventskalender.php';
    $yourcat = get_category($yourcat->parent);
  } while (true);

  // not found in the parent hierarchy, return the default template:
  return $archive_template;

The parameter of this function is the default template that would be applied without the hook implemented here. We have to detect if we’re in a matching category and if so, overwrite that template by returning our customized one. Therefore the loop from the current category up to the root category and search for our anchor category which is the root for the categories we want to apply the custom template for.

If found, we return the custom template file, otherwise we return the default one.

Now this function has to be registered, and that’s it:

add_filter( 'category_template', 'get_advent_category_template' ) ;

How the WordPress Template system could be extended

When I stumbled over this issue I asked in the WordPress IRC channel after not seeing a proper solution, where I was told that it is a quite common problem, and more people asked for a solution in the past.

In fact it would be quite easy to extend the WordPress template system by another template „class“ in the second line of the graphical overview. Inserting another variant between category-$id.php and category.php would fit nicely from a logical point of view.

A Category archive that is defined for a single category is preferred (left of the new one), but the tree-based category template (the new one) is preferred over the generic category template.