I feel like you may be adding mystery where there really isn’t any.
- Add the custom fields
- In the template: Get the field values
- Display values as desired.
Fields aren’t displayed automatically because there is no way to predict how a developer wants to handle or display values. Getting values depends on the options framework but ACF is pretty straightfoward: See the docs here. Use get_field() for each field then output the value as desired.
If you are inside a WP loop you don’t have to specify the post id, it will use the current global post setup by the loop. Outside the WP loop you should specify which post you want to retrieve the value of.
Obviously the output will require some basic knowledge of html, css, php, and WP templating; You are, after all, creating a custom template. But if you know exactly how you want to display the data it shouldn’t be difficult to figure that part out.
Also if you have any particular code that you feel should be working but isn’t for whatever reason, you can post the relevant bits and we can look it over and see if we can figure out what’s going on.
I was hoping to just include the CFs in the loop – then I can style it as desired.
As linked in the post, this is the code. I added this to theme functions.
add_action( 'loop_start', 'before_single_post_content' );
function before_single_post_content() {
if ( is_singular( 'post') ) {
$cf = get_post_meta( get_the_ID(), 'suppliers', true );
if( ! empty( $cf ) ) {
echo '<div class="before-content">'. $cf .'</div>';
}
}
}
Not is_singular( 'post')
, more like is_singular('suppliers')
Try using the get_field() function I linked above instead of get_post_meta(). I can’t remember how ACF stores fields but it is possible that the method of storage doesn’t map 1:1 in post meta with the exact key you specified in the field.
-
This reply was modified 8 years, 1 month ago by
csloisel.
Definately closer – code as below give me the phone number field and shows it just above the comment box.
add_action( 'comment_form_before', 'after_single_post_content' );
function after_single_post_content() {
if ( is_singular( 'suppliers') ) {
$cf = get_post_meta( get_the_ID(), 'main_office_phone', true );
if( ! empty( $cf ) ) {
echo '<div class="after-content">'. $cf .'</div>';
}
}
}
Can I get this just below the title? (basically inside the content area)
How do I add more than one CF? Like this? or duplicate the whole set of code for each?
$cf = get_post_meta( get_the_ID(), 'main_office_phone', 'contact_person', true );
@csloisel – I’m working on using the loop at this point. See previous comments for progress made. Thanks for your help.
I would start by doing this output in the post type single template. Doing this via a hook is adding an unnecessary layer of complexity here, giving you limited control on placement and is simply not intuitive.
If you don’t have a template already it’s pretty easy to set one up. Just start out by adding a single-suppliers.php into your theme root. Then copy everything from single.php and paste it into the new file. However, you don’t want to do this on a pre-made theme as you won’t be able update it without wiping out your changes. So if you are using a pre-made theme make sure you do this in a child theme.
As for getting multiple fields you want to repeat the get_post_meta()
call for each field using a unique variable for storage, it won’t retrieve more than one at a time.
For example:
$office_phone = get_post_meta( get_the_ID(), 'main_office_phone', true );
$contact_person = get_post_meta( get_the_ID(), 'contact_person', true );
I’d love to do it in the template. I’ll add one… but not sure what part to swap out or add…