• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

How do I add a class to the genesis attribute entry?

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › Design Tips and Tricks › How do I add a class to the genesis attribute entry?

This topic is: resolved

Tagged: genesis-theme-framework, loop, php, taxonomy

  • This topic has 16 replies, 3 voices, and was last updated 10 years, 9 months ago by te1.
Viewing 17 posts - 1 through 17 (of 17 total)
  • Author
    Posts
  • April 23, 2015 at 3:44 am #148876
    te1
    Member

    Hi there,

    How do I add a class to the genesis attribute entry? I'm trying to add a class to the article depending on which post/social feed it is using the get_terms taxonomy. My test post is being displayed (the first/latest one) but it doesn't have the class of Twitter, for example, in the case of this argument. I've tried adding the variable $typename as a class/attribute to the article but it's not working.

    Here is the code:

    gist.github.com/telliott1/3c6e9423f9fda259507a

    Cheers

    http://boxchilli.co/y-services/young-people
    April 23, 2015 at 3:18 pm #148932
    Ren Ventura
    Member

    You can use the genesis_attr_{context} filter. For example:

    /**
     *	Add a custom ID to the .content container
     */
    add_filter( 'genesis_attr_content', 'rv_attr_content' );
    function rv_attr_content( $attr ) {
    	$attr['id'] = 'my-id';
    	return $attr;
    }
    

    This article will help you with the contexts available to use:

    http://www.rfmeier.net/using-genesis_markup-with-html5-in-genesis-2-0/


    Web & Software Developer & Blogger | RenVentura.com | Follow Me on Twitter @CLE_Ren

    April 23, 2015 at 3:44 pm #148938
    Badlywired
    Member

    Having read your code, I think this is the solution

    printf( '<article %s>', genesis_attr( 'entry', array('class' =>$typename )) );

    ass genesis attr takes an array of any attribute, not just class


    My techy blog WordPress and stuff badlywired.com

    April 23, 2015 at 4:22 pm #148947
    Ren Ventura
    Member

    Apparently I overlooked the link to your code. My apologies. That considered, use @Badlywired's suggestion.


    Web & Software Developer & Blogger | RenVentura.com | Follow Me on Twitter @CLE_Ren

    April 23, 2015 at 4:30 pm #148949
    Badlywired
    Member

    🙂


    My techy blog WordPress and stuff badlywired.com

    April 24, 2015 at 2:56 am #148990
    te1
    Member

    @Ren Ventura Yes that worked as I've managed to add a class of 'test' to the article entry, but how do I add a class using the variable $typename?

    $terms = get_terms( 'sa_tax_sources' );
    $typename = $terms[0]->name;

    Any help is much appreciated.

    April 24, 2015 at 3:04 am #148992
    Badlywired
    Member

    OK ignore my answer that is right and try rens, that he already said doesn't apply

    --------------------------

    by the way ...

    More of a wordpress question rather than Genesis, however I think you are using the wrong function as you are in the loop and I assume you want the taxonomy term used on that specific post, not all of them?

    have a look at wp_get_post_terms()

    https://codex.wordpress.org/Function_Reference/wp_get_post_terms

    which will return an array of terms for the post and then you will need to process that, maybe with implode


    My techy blog WordPress and stuff badlywired.com

    April 24, 2015 at 3:11 am #148995
    te1
    Member

    Maybe something like this?

    add_filter( 'genesis_attr_entry', 'custom_attributes_entry' );
    function custom_attributes_entry( $attributes ) {
    	$attributes['class'] .= ' ', array( 'class' => $typename);
    	return $attributes;
    }
    April 24, 2015 at 3:14 am #148996
    te1
    Member

    @Badlywired I tried your code first but it didn't work? It didn't break anything but nothing showed up...

    "I assume you want the taxonomy term used on that specific post, not all of them?" - Yes, this is what I'm trying to achieve.

    I appreciate your help.

    April 24, 2015 at 3:21 am #148998
    Badlywired
    Member

    If you go the filter route

    something like

    add_filter( 'genesis_attr_entry', 'custom_attributes_entry' );
    function custom_attributes_entry( $attributes ) {
    
         global $post;
    
       // your code  which I think is wrong as it returns all terms
      $terms = get_terms( 'sa_tax_sources' );
       $typename = $terms[0]->name;
      // end of your code
    
    	$attributes['class'] .= ' ', array( 'class' => $typename);
    	return $attributes;
    }

    my version would be (assuming you only want the terms for that post

    add_filter( 'genesis_attr_entry', 'custom_attributes_entry' );
    function custom_attributes_entry( $attributes ) {
    
         global $post;
    
       // my code - but I am guess what you really are trying to do
       $terms = wp_get_post_terms( $post->ID, 'sa_tax_sources' );
       $typename = implode(' ',$terms);
      // end of your code
    
    	$attributes['class'] .= ' ', array( 'class' => $typename);
    	return $attributes;
    }

    My techy blog WordPress and stuff badlywired.com

    April 24, 2015 at 3:29 am #148999
    te1
    Member

    @Badlywired Right, I'll give that a go...

    April 24, 2015 at 3:38 am #149000
    te1
    Member

    @Badlywired I tried using your version but I get this error? PHP Parse error: syntax error, unexpected ',' in /wp-content/themes/genesis-sample/functions.php on line 222

    April 24, 2015 at 3:43 am #149001
    Badlywired
    Member

    Come on @tel thats your code error that I just copied from your post!!

    $attributes['class'] .= ' ', array( 'class' => $typename);

    spot the comma that should be a dot!

    $attributes['class'] .= ' '. array( 'class' => $typename);


    My techy blog WordPress and stuff badlywired.com

    April 24, 2015 at 3:47 am #149002
    te1
    Member

    @Badlywired I now get this error: PHP Catchable fatal error: Object of class stdClass could not be converted to string in /wp-content/themes/genesis-sample/functions.php on line 219

    April 24, 2015 at 3:58 am #149003
    Badlywired
    Member

    @te1 unfortunately I can't debug your code remotely, or free. I think you have enough pointers to get on with it.
    I'm sure you are a competent coder judging by the code you posted initially.


    My techy blog WordPress and stuff badlywired.com

    April 24, 2015 at 4:06 am #149004
    te1
    Member

    @Badlywired Ok, I understand. Yes, thank you for your pointers, they're a great help.

    April 24, 2015 at 9:42 am #149032
    te1
    Member

    @Badlywired I figured it out! Thanks for your pointers towards this.

    Here is the working code:

    add_filter( 'genesis_attr_entry', 'custom_attributes_entry' );
    function custom_attributes_entry( $attributes ) {
    	global $post;
    
    	$terms = wp_get_post_terms( $post->ID, 'sa_tax_sources', array('fields' => 'slugs') );
    	$typename = implode(' ', $terms);
    
    	$attributes['class'] .= ' '. $typename;
    	return $attributes;
    }
  • Author
    Posts
Viewing 17 posts - 1 through 17 (of 17 total)
  • The topic ‘How do I add a class to the genesis attribute entry?’ is closed to new replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2026 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble