Community Forums › Forums › Archived Forums › Design Tips and Tricks › How do I add a class to the genesis attribute entry?
Tagged: genesis-theme-framework, loop, php, taxonomy
- This topic has 16 replies, 3 voices, and was last updated 9 years, 7 months ago by te1.
-
AuthorPosts
-
April 23, 2015 at 3:44 am #148876te1Member
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-peopleApril 23, 2015 at 3:18 pm #148932Ren VenturaMemberYou 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 #148938BadlywiredMemberHaving 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 #148947Ren VenturaMemberApparently 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 #148949BadlywiredMemberApril 24, 2015 at 2:56 am #148990te1Member@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 #148992BadlywiredMemberOK 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 #148995te1MemberMaybe 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 #148996te1Member@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 #148998BadlywiredMemberIf 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 #148999te1Member@Badlywired Right, I'll give that a go...
April 24, 2015 at 3:38 am #149000te1Member@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 #149001BadlywiredMemberCome 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 #149002te1Member@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 #149003BadlywiredMember@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 #149004te1Member@Badlywired Ok, I understand. Yes, thank you for your pointers, they're a great help.
April 24, 2015 at 9:42 am #149032te1Member@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; }
-
AuthorPosts
- The topic ‘How do I add a class to the genesis attribute entry?’ is closed to new replies.