Community Forums › Forums › Archived Forums › General Discussion › Insert custom field into page template using Advanced Custom Field
Tagged: advanced custom field
- This topic has 6 replies, 2 voices, and was last updated 10 years ago by Tony @ AlphaBlossom.
-
AuthorPosts
-
April 3, 2014 at 11:35 am #98319japser123Member
Hi there,
I'm quite new to Genesis and I'm running into some problems placing field data derived from the Advanced Custom Field plugin in the correct location.
I've started a new page template like this:
<?php
/*
Template Name: location
*/
?>
<?php genesis(); ?>By inserting <p><?php the_field('field_name'); ?></p> above <?php genesis(); ?> the field data shows up but is not placed into the Content div but way in top of the code. How can I correct this?
Thank you,
JasperApril 3, 2014 at 12:15 pm #98325Tony @ AlphaBlossomMemberHello,
There are several ways you can do this, but using your example above you will have to use a "hook" to place it in the proper location, such as:
add_action( 'genesis_entry_content', 'abte_my_custom_function' ); function abte_my_custom_function() { // your acf code here }
You can reference the docs for various hook options and note that some hooks are depenendant on whether you're using HTML5 or not.
If that's the only change you're making to this page, you could add your function to your functions.php file instead of creating a page template, and use a conditional statement to load only on your locations page.
Tony Eppright | http://www.AlphaBlossom.com | Follow me on twitter @_alphablossom
April 3, 2014 at 3:57 pm #98353japser123MemberI'm probably more of a newbie than you expected since I don't get this to work 🙁
Following your answer my template file now looks like the code below but the field data is still entered at the top of the page and not in the content div.
What is this 'abte_my_custom_function' in your example? Am I supposed to reference to that in the functions.php?
<?php
/*
Template Name: location
*/
?>
add_action( 'genesis_entry_content', 'abte_my_custom_function' );
function abte_my_custom_function() {
<p><?php the_field('field_name'); ?></p>
}
<?php genesis(); ?>April 3, 2014 at 4:06 pm #98354Tony @ AlphaBlossomMemberHello,
No problem at all. The abte_my_custom_function is the name of your new function, so in the add_action you're saying to add your function (which is called abte_my_custom_function but you can rename it to whatever you want) to the genesis_entry_content hook. You could add this whole code to your functions.php file or here, but not both. If you add it to your functions.php you would have to add some extra code, a conditional statement to only do this on the location page.
Your code is missing some opening/closing php tags. Try this:
<?php /* Template Name: location */ ?> <?php add_action( 'genesis_entry_content’, ‘abte_my_custom_function' ); function abte_my_custom_function() { ?> <p><?php the_field( 'field_name' ); ?></p> <?php } ?> <?php genesis(); ?>
Depending on where you want to place it, you will have to change "genesis_entry_content" to the right hook (reference the link above to see all of the hook locations). Here's a great reference to see which hook to use:
http://genesistutorials.com/visual-hook-guide/2012/01/01/threaded-comments/
Tony Eppright | http://www.AlphaBlossom.com | Follow me on twitter @_alphablossom
April 3, 2014 at 4:22 pm #98358Tony @ AlphaBlossomMemberSorry, the ' marks did not get copied over right:
<?php /* Template Name: location */ ?> <?php add_action( 'genesis_entry_content', 'abte_my_custom_function' ); function abte_my_custom_function() { ?> <p><?php the_field( 'field_name' ); ?></p> <?php } ?> <?php genesis(); ?>
Tony Eppright | http://www.AlphaBlossom.com | Follow me on twitter @_alphablossom
April 4, 2014 at 11:07 am #98484japser123MemberFantastic! It's working now, thank you!
April 4, 2014 at 11:25 am #98486Tony @ AlphaBlossomMemberGreat, glad to be of help. Have a great weekend!
Tony Eppright | http://www.AlphaBlossom.com | Follow me on twitter @_alphablossom
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.