One custom post type that i've created has 5 custom fields that display specific content for a template that displays them in different areas on the page.

My question is if i would have to create another loop for each custom field i'm referencing on my template or if one would suffice?


  • Are you displaying the custom field in the same template? (say single.php) – Dan Ștefancu Sep 24 '13 at 22:16
  • Depends on where you want to display the fields, but probably not. Without more detail this is going to be hard to answer. – s_ha_dum Sep 24 '13 at 23:34
  • @DanȘtefancu yes i'm displaying the custom fields in the same template. – MARS Sep 25 '13 at 13:34

For all intents and purposes, you only need one loop. While s_ha_dum's comment is correct, odds are since you're asking this question, you won't have a need for multiple loops.

<? get_header(); ?>
    <? if(have_posts()): while(have_posts()): the_post(); ?>

        <!--Your content here, like so-->
        <div id="content">
            <? the_field('body_content'); ?>
            <? the_field('footer_content'); ?>

    <? endwhile; endif; ?>
<? get_footer(); ?>

This will suffice your needs. Of course you would just remove my loop contents and put your own in place. Just giving an example.

  • ok i see that one loop is sufficient. What seems to be the million dollar question in my mind (and i'm starting to think i'm approaching it the wrong way to begin with) is that i really just don't understand how i can link to a page that has a template of Single-neighborhood that calls on the custom fields associated with a CPT of Single Neighborhood and have Wordpress know which post to call. Since i can't put a specific taxonomy or page ID into the loop because it's supposed to generate the content dynamically not from a specific page or post. – MARS Sep 25 '13 at 13:38

You could also keep it lean and grab all the custom fields in a single query using get_post_custom():


For example:

$custom_fields = get_post_custom();

// access the array of fields however you like
  • But how will WP know which post to render, these aren't like general category posts they are specific neighborhoods with custom content on each so how do i call a specific neighborhood in a loop that sits within a Single-neighborhood.php template that needs to be used on 27 other neighborhoods? That's what's hard for me to understand is the "how". I mean does WordPress take into consideration the link that you came from in order to determine the page that you're going to in order to know which post of the CPT Single Neighborhood (in my case) to display? Does that make sense? – MARS Sep 25 '13 at 13:42
  • Assuming your post type is neighborhood, your theme's single-neighborhood.php file will load whenever a neighborhood entry is viewed. For example, assuming your post type rewrite is neighborhoods, if you create a new neighborhood entry called "Sanborn", with a URL of www.yourdomain.com/neighborhoods/sanborn/ then single-neighborhood.php is loaded from your theme to determine what that page looks like. Each page has a unique ID, and the_post() loads in the data for the the ID of the current post/page/post type. – Kevin Leary Sep 26 '13 at 14:40
  • that worked perfectly thank you. Just one more question would it matter if the post type rewrite were singular? Would that have some affect down the road or is it just best practice to keep it plural? – MARS Sep 26 '13 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.