Community Forums › Forums › Archived Forums › Design Tips and Tricks › Custom Field for genesis_after_header
Tagged: custom field, Outreach Pro
- This topic has 7 replies, 3 voices, and was last updated 8 years, 3 months ago by wildwebwest.
-
AuthorPosts
-
September 4, 2016 at 7:39 pm #192599wildwebwestMember
Hey friends: I'm trying to use custom fields that seem like would be fairly straightforward.
This code is now located in my function.php file:
//* Hook mbr custom shortcode content after header
add_action('genesis_after_header', 'mbr_after_header');
function mbr_after_header() {
if ( is_singular( 'post' ) )
$cf = get_post_meta( $post_id, 'mbr_customfield', true );
if(empty($cf)) {
echo "Empty Customfield";
} else {
echo "".$cf."";
}
}My goal is to use the mbr_customfield name to place a unique slider revolution shortcode in the value so that a unique custom slider resolves "after_header" for each page.
So I thought maybe the shortcode was the problem. I replaced the slider revolution shortcode with plain text for testing.
You'll see on this page: http://87a.b45.myftpupload.com/backcountry-wedding-destination/ that my hook code at the top right under the header, is echoing "Empty Customfield" but should be echoing whats in the mbr_customfield value which is; Full Width Slider Revolution Shortcode after header will go here
I'm not sure if this is the appropriate portal to get some help, but if someone can steer me into the right direction, I'd like to be able to use custom fields to accommodate custom sliders "after_header" for each page in this upgrade. It would be really sweet. Just got to correct the code above because its not echoing whats in that field.
After I figure that out, I'll want to replace plain text with rev slider shortcode so if some other snippet is necessary, I"ll open a separate support thread.
Thanks much for anyone who can help me out.
Kind regards, mary
Full Width Slider Revolution Shortcode after header will go here
Wild Web West, LLC
#IHeartGenesisSeptember 5, 2016 at 5:44 am #192610Victor FontModeratorWhere are you retrieving post ID? get_post_meta is designed to retrieve custom fields within the WordPress loop. To display a custom field outside of the loop, you have to do something a little different:
/* Hook mbr custom shortcode content after header add_action('genesis_after_header', 'mbr_after_header'); function mbr_after_header() { global $wp_query; $post_id = $wp_query->post->ID; if ( is_singular( 'post' ) ) { $cf = get_post_meta( $post_id, 'mbr_customfield', true ); if(empty($cf)) { echo "Empty Customfield"; } else { echo "".$cf.""; } wp_reset_query(); } }
Even though you are echoing the shortcode, it may be get displayed as text and the shortcode may not actually execute. You should probably execute the shortcode in do_shortcode($cf).
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet?September 5, 2016 at 6:48 am #192619Brad DaltonParticipantTo output custom field values outside the loop, I would use get_the_ID() like this:
add_action('genesis_after_header', 'slider_after_header'); function slider_after_header() { if ( is_singular( 'post' ) ) { $cf = get_post_meta( get_the_ID(), 'slider', true ); echo do_shortcode( $cf ); } }
Rather than the global variable.
Or
Store the id as a variable:
add_action('genesis_after_header', 'slider_after_header'); function slider_after_header() { if ( is_singular( 'post' ) ) { $postid = get_the_ID(); $cf = get_post_meta( $postid, 'slider', true ); echo do_shortcode( $cf ); } }
This solution is tested and works.
September 5, 2016 at 6:59 am #192620September 5, 2016 at 8:46 am #192629wildwebwestMemberDear Brad and Victor,
Thank you sooooo much for your assistance with this slightly advanced custom fields issue I was having. I can't thank you enough.
Due to shorter code, I did end up using Brads first iteration which worked like a charm. I figured shorter code, faster results. But all three examples are so helpful in learning and skill enhancements.
I'm flying with the geese!
Have a wonderful day and again, muchos gracias, domo arigato, THANK YOU!!!
Mary
Wild Web West, LLC
#IHeartGenesisSeptember 5, 2016 at 9:02 am #192632wildwebwestMemberOooh. One more question.
Brad, I would like to be able to use that custom field for both posts AND pages.
The code works perfectly on my post at http://87a.b45.myftpupload.com/making-wedding-memories-at-mackay-bar-ranch/
But is not calling the full width slider shortcode on a 'page' like here: http://87a.b45.myftpupload.com/hunting/
Any suggestions?
Wild Web West, LLC
#IHeartGenesisSeptember 5, 2016 at 9:11 am #192634Brad DaltonParticipantChange the conditional to work with pages
if ( is_singular(array( 'post', 'page' ) ) ) {
September 5, 2016 at 9:22 am #192636wildwebwestMemberBravo! Thank you so much Brad! You are awesome! That worked and once again, I can't thank you enough for your excellence and willingness to help other wordpress users!
Kind regards,
Mary
Wild Web West, LLC
#IHeartGenesis -
AuthorPosts
- The topic ‘Custom Field for genesis_after_header’ is closed to new replies.