• Skip to primary navigation
  • Skip to main content
  • Skip to footer

JH Tech Services

A knowledgebase

  • Portfolio
  • Knowledgebase
  • About

Genesis dynamic filters

June 14, 2013 by jer0dh

I’m impressed with Genesis. It’s amazing what can be done with filters and hooks. (Using Genesis 2.0-Beta2)

Today, I’m working on my personal site to learn this framework and I discovered how easy it was to add a class to any section of the website. I looked at the loop and saw that it used a function genesis_att to add the attributes of the html for the entry-content. In this function it uses the following:


function genesis_parse_attr( $context, $attributes = array() ) {

    $defaults = array(
        'class' => sanitize_html_class( $context ),
        );
    $attributes = wp_parse_args( $attributes, $defaults );
    //* Contextual filter
    return apply_filters( 'genesis_attr_' . $context, $attributes, $context );

I love how it dynamically creates filters by concatenating $context with the prefix, ‘genesis_attr_’. All you need to add a class is the $context..

I needed to have a class “withImage” on the entry-content div only when the post contains an image. Here’s what I did


add_filter('genesis_attr_entry-content','pb_add_content_class');
function pb_add_content_class($attributes) {

// test if has image:
    global $post;
    $args = array(
        'post_type' => 'attachment',
        'numberposts' => -1,
        'post_status' => null,
        'post_parent' => $post->ID
        );
    $attachments = get_posts( $args );
    if ( $attachments ) {
          $attributes['class'] .= ' withImage';
          }

  return $attributes;
  }
  • Genesis
  • Knowledgebase

Footer

  • Portfolio
  • Knowledgebase
  • About

© 2021