Feature
Post

Category
Code


Using Filters With Conditional Tags in Child Themes

On a recent project using a Genesis child theme I found myself in a situation where I wanted to use a filter, but only on certain views, namely the category view and the homepage view. Adding a filter is pretty straight forward, but using that filter in combination with conditional tags you need to add a bit extra instead of just the tags themselves.

Now, the example is for the Genesis Framework, but really the logic behind will work on any child theme using filters. What I wanted to filter was the output of the post meta area, which normally displays both the categories and the tags. I wanted to use a slightly different post meta on the homepage where the tags would be replaced with a Continue reading link.

This is what the filter looks like:

PHP:
  1. //Customizing Post Meta 
  2. function forsite_post_meta_filter($post_meta) {
  3. $post_meta = '[post_categories], <a href="'. get_permalink() .'" title="'. the_title_attribute('echo=0') .'">Continue Reading</a>';
  4. return $post_meta;
  5. }

As you can see I am just adding a permalink with a 'Continue Reading' text. Nothing too fancy. Normally you would add your filter and you'd be all done. Like so:

PHP:
  1. //Customizing Post Meta 
  2. function forsite_post_meta_filter($post_meta) {
  3. $post_meta = '[post_categories], <a href="'. get_permalink() .'" title="'. the_title_attribute('echo=0') .'">Continue Reading</a>';
  4. return $post_meta;
  5. }
  6.  
  7. add_filter('genesis_post_meta', 'forsite_post_meta_filter');

Using Conditial Tags with this filter actually requires the use of an add_action statement. The way to do is by declaring another function which handles the conditional tags. Something like this

PHP:
  1. function forsite_post_meta_conditionals() {
  2. if( is_home() || is_category() ) {
  3. add_filter('genesis_post_meta', 'forsite_post_meta_filter');
  4. }
  5. }
  6.  
  7. add_action('wp', 'forsite_post_meta_conditionals');

The if statement determines to actually use this filter only whether you are looking at the home page or the category view. Combined the full code looks like this:

PHP:
  1. //Customizing Post Meta 
  2. function forsite_post_meta_filter($post_meta) {
  3. $post_meta = '[post_categories], <a href="'. get_permalink() .'" title="'. the_title_attribute('echo=0') .'">Continue Reading</a>';
  4. return $post_meta;
  5. }
  6. function forsite_post_meta_conditionals() {
  7. if( is_home() || is_category() ) {
  8. add_filter('genesis_post_meta', 'forsite_post_meta_filter');
  9. }
  10. }
  11. add_action('wp', 'forsite_post_meta_conditionals');

More info on conditional tags can be found in the Codex, and more information on WordPress filters. Have you worked with conditional tags and / or filters before?


  1. By Tim Gordon posted on May 27, 2010 at 10:22 am
    Want an avatar? Get a gravatar! • You can link to this comment

    Very useful post. Thanks a lot.

  2. TrackbackConditional Logic and Filters