Community Forums › Forums › Archived Forums › General Discussion › Add code to post based on tags?
- This topic has 6 replies, 3 voices, and was last updated 8 years, 7 months ago by danworld.
-
AuthorPosts
-
August 30, 2015 at 2:33 am #164042danworldMember
The WordPress codex has this code to use:
<?php if ($all_the_tags); $all_the_tags = get_the_tags(); foreach($all_the_tags as $this_tag) { if ($this_tag->name == "sometag" ) { ?> <p>SOME HTML CODE <img src="someimage.jpg"></p> <?php } else if ($this_tag->name == "someothertag" ) { ?> <p>SOME OTHER HTML CODE <img src="someotherimage.jpg"></p> <?php } else { // it's neither, do nothing ?> <!-- not tagged as one or the other --> <? } } } ?>
I just did a simple change to add some code when the tag is "cardboard". And I added it in the Genesis Simple Hooks plugin in genesis_entry_content. But it doesn't seem to have any effect. Your help is appreciated. Below is my code with the simple modification:
<?php if ($all_the_tags); $all_the_tags = get_the_tags(); foreach($all_the_tags as $this_tag) { if ($this_tag->name == "cardboard" ) { ?> <p>Cardboard!</p> <?php } else if ($this_tag->name == "someothertag" ) { ?> <p>SOME OTHER HTML CODE <img src="someotherimage.jpg"></p> <?php } else { // it's neither, do nothing ?> <!-- not tagged as one or the other --> <? } } } ?>
Thank you!
https://codex.wordpress.org/Function_Reference/get_the_tagsAugust 30, 2015 at 5:28 am #164043Brad DaltonParticipantLink to your site please.
Where did you want to execute the code exactly?
August 30, 2015 at 6:35 am #164046Victor FontModeratorYou may have an error in your code. You are using the php open short tag, <?, as your last php open tag. The open short tag is generally turned off by default in most php installations. Change it to <?php and see if it makes a difference.
Also, with simple hooks, make sure the execute php checkbox is checked. I forget to do that sometimes.
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet?August 31, 2015 at 6:32 am #164115danworldMemberI appreciate the quick replies, but still no luck.
@braddalton First let me say I have gotten lots of value from your site and responses in these forums, so, thank you.To answer your questions, I can't link to the site because I'm developing it on a local server. And I wanted to execute the code with the rest of the custom code I’m putting in genesis_entry_content.
I appreciate the ideas. I thought the code should be right since it’s just copy/pasted from the WordPress codex. I tried but changing the php tag didn’t seem to help. And I did check the execute php checkbox; I learned from that mistake already.
August 31, 2015 at 6:54 am #164118Victor FontModeratorYou might want to try placing the call to the function above the if statement like this:
<?php $all_the_tags = get_the_tags(); if ($all_the_tags); foreach($all_the_tags as $this_tag) { if ($this_tag->name == "cardboard" ) { ?> <p>Cardboard!</p> <?php } else if ($this_tag->name == "someothertag" ) { ?> <p>SOME OTHER HTML CODE <img src="someotherimage.jpg"></p> <?php } else { // it's neither, do nothing ?> <!-- not tagged as one or the other --> <?php } } } ?>
The reason why is that the if statement will always evaluate as false, and therefore not execute, unless the $all_the_tags variable has content. It doesn't have any content until it is populated by get_the_tags().
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet?August 31, 2015 at 8:38 pm #164184Brad DaltonParticipantWhy can't you use something like from your functions file:
add_action( 'genesis_entry_content','your_function' ); function your_function() { if ( has_tag( 'cardboard') && is_singular('post') ) { echo'execute your code'; } }
To execute before the content, change the hook.
Or
add_action( 'genesis_entry_content','your_function' ); function your_function() { if ($all_the_tags); $all_the_tags = get_the_tags(); foreach($all_the_tags as $this_tag) { if ($this_tag->name == "cardboard" ) { ?> <p>SOME HTML CODE </p> <?php } else if ($this_tag->name == "someothertag" ) { ?> <p>SOME OTHER HTML CODE </p> <?php } else { // it's neither, do nothing ?> <!-- not tagged as one or the other --> <? } } }
September 2, 2015 at 4:03 am #164307danworldMemberif ( has_tag( 'cardboard') && is_singular('post') ) { echo'execute your code';
Yes, that does the job!
Thanks!
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.