• I’ve a feel this should be really simple, but I’ve google and searched and cannot find the solution for what feels like it should be a simple issue.

    I’ve created a custom post type called ‘suppliers’. “Used Custom Post Type UI”
    Added a bunch of custom fields including ‘supplier_main_phone’. Used
    Advanced Custom Fields

    Now I’m wanting to be able to see these on the front end – however when I go to ‘View Supplier’ all I see are the core WP fields.

    I’ve looked at ways to supposedly add the CFs to the loop (loop_start) using a theme function – https://wpsites.net/web-design/how-to-create-a-custom-field/ (nothing happening)
    And how to modify add a single-suppliers template (displays outside the main content area)

    Feeling silly, but all I want is a way to display CPT with their CF on the front end…

    Any ideas or tutorials you can point me to?

Viewing 9 replies - 1 through 9 (of 9 total)
  • I feel like you may be adding mystery where there really isn’t any.

    1. Add the custom fields
    2. In the template: Get the field values
    3. 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.

    Thread Starter Bizstudio

    (@ajamm)

    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>';
        }
      }
    }
    Moderator bcworkz

    (@bcworkz)

    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 7 years, 11 months ago by csloisel.
    Thread Starter Bizstudio

    (@ajamm)

    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 );

    Thread Starter Bizstudio

    (@ajamm)

    @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 );
    Thread Starter Bizstudio

    (@ajamm)

    I’d love to do it in the template. I’ll add one… but not sure what part to swap out or add…

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Displaying Custom Fields and Custom Post types’ is closed to new replies.