Community Forums › Forums › Archived Forums › Design Tips and Tricks › Use add_action conditionally for custom function
Tagged: add_action, conditionals
- This topic has 6 replies, 3 voices, and was last updated 8 years ago by SelenaD.
-
AuthorPosts
-
February 9, 2016 at 7:48 pm #178698SelenaDMember
I have a function that calls some custom fields. I want to add that function in a different place based on whether it's the home page, interior page, or blog page. So here's what I want to accomplish:
function rm_some_function() { the_field('some_field'); } function rm_position_some_function() { if(is_front_page()) { add_action('genesis_before_header', 'rm_some_function', 15); } if(is_home_page()) { add_action('genesis_after_header','rm_some_function', 15); } } add_action ('genesis_before_loop', 'rm_position_some_function');
But it doesn't work. Any help would be appreciated.
Thanks!
February 9, 2016 at 9:18 pm #178701ChristophMemberHi,
you have a typo.
It´s is_home , not is_home_page.
February 9, 2016 at 9:28 pm #178702SelenaDMemberThanks Christoph - but that isn't my issue. That's just my tired brain giving sample code in the forum. 🙁
Here's real code:
function rm_bio_box() { ?> <div class="rm-bio-box"><div class="wrap"> <div class="rm-bio-image"> <?php the_field('bio_image', 'option'); ?> </div> <div class="rm-bio-text-area"> <div class="rm-bio-name"> <?php the_field('bio_name', 'option'); ?> </div> <div class="rm-bio-title"> <?php the_field('bio_title', 'option'); ?> </div> <div class="rm-bio-text"> <?php the_field('bio_text', 'option'); ?> </div> </div> </div></div> <?php } function rm_bio_box_position() { if(is_front_page()) { add_action ('genesis_before_content_sidebar_wrap', 'rm_bio_box', 15); } } add_action('genesis_before_loop', 'rm_bio_box_position');
February 10, 2016 at 8:37 am #178736Victor FontModeratorSince this is an issue with ACF and not a Studio Press theme, you may be better off asking your question in the ACF forum. In any case, you may be using the_field incorrectly. http://www.advancedcustomfields.com/resources/the_field/
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet?February 10, 2016 at 8:41 am #178737SelenaDMemberThanks Victor, but the ACF portion works just fine. The issue is ONLY with using the add_action as a conditional. It just so happens that I'm using ACF in this particular case.
February 10, 2016 at 12:08 pm #178749ChristophMemberHi,
genesis_before_loop "fires" after genesis_before_content_sidebar_wrap
There is nothing "left" that it could add the action to.Try genesis_after_header instead of genesis_before_loop
March 2, 2016 at 8:19 am #180441SelenaDMemberChristoph, you're a saint. Knowing when things fire is a definite gap in my knowledge. It took me awhile to come back to this, but I am very grateful!
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.