Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm creating a template but every time I add a new custom post type (in this example neighborhood) it keeps injecting a new <ol> and <li> here is the code I currently have and I'm using the ACF Advanced Custom Fields plugin to create my custom fields.

<?php

/*

  Template Name: Neighborhoods Page

*/

include(TEMPLATEPATH."/header-internal.php");?>

  <div class="container-space"></div>

    <div class="container-main">

    <?php

    $args = array(
        'post_type' => 'neighborhoods'
    );

    $the_query = new WP_Query( $args );

    ?>


    <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

      <ol class="col-neigh-title neighborhood">
        <li><a href="<?php the_field( 'url_neighborhood' ); ?>"><?php the_field( 'neighborhood_name' ); ?></a>
        </li>
      </ol>

    <?php endwhile; else: ?>

    <p>There are no posts or pages here</p>

    <?php endif; ?>

    </div>


<?php get_footer(); ?>
share|improve this question
    
I don't understand i paste my code in one way and this forum formats it a different way this is not my code and i won't be able to illustrate my point accurately if it does this. –  MARS Sep 18 '13 at 21:12
    
There's the WYSIWYG editor that you should use. The "forum" (which is no forum, but a Q/A site) itself uses MarkDown to format content. –  kaiser Sep 18 '13 at 21:19
1  
you're inserting a new ol and li for each post in the loop, what do you expect to happen? –  Milo Sep 18 '13 at 21:21
    
@MARS Please edit your question. I can't read a question in there. Your comment to Milo didn't help clarifying it. –  kaiser Sep 18 '13 at 21:28
    
yeah i'm posting my question and it gets formatted in a non-clear way. I want to keep adding <li> elements because essentially it's a list of neighborhoods but if i don't state that's it's an <li> within an <ol> from the beginning how will the template know that's what i want inserted. I see based on Milo's post that it keeps inserting a new OL and LI each time a post or Neighborhood (in this case is added), how do i avoid that but still have it format into a list? Is that better? –  MARS Sep 18 '13 at 21:32

2 Answers 2

If I understand your question, the issue is that anything within the while loop will get repeated for each post. If you want to wrap all posts in a single set of <ol></ol> tags, those must go outside that while statement.

if ( $the_query->have_posts() ) :

    // open the <ol> outside the loop
    echo '<ol class="col-neigh-title neighborhood">';

    // anything inside here is repeated for each post
    while ( $the_query->have_posts() ) :
        $the_query->the_post();
        ?>

        <li><a href="<?php the_field( 'url_neighborhood' ); ?>"><?php the_field( 'neighborhood_name' ); ?></a></li>

        <?php
    endwhile;

    // close the <ol> outside the loop
    echo '</ol>';

endif;
share|improve this answer
    
good thought, didn't work for me though. I am that new though that i didn't know that's how the loop worked. You gave me food for thought though and for that thank you. –  MARS Sep 18 '13 at 22:37
    
I guess I don't understand the question then! –  Milo Sep 18 '13 at 23:35
    
I figured out the solution using your concept of moving the <ol> out of the loop and figured it out, I don't know if it's good practice to edit the original post to insert the solution? Thanks Milo. –  MARS Sep 19 '13 at 13:48
'neighborhoods' ); $the_query = new WP_Query( $args ); ?>
    <ol class="col-neigh-title neighborhood">

    <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>


        <li><a href="<?php the_field( 'url_neighborhood' ); ?>"><?php the_field( 'neighborhood_name' ); ?></a>
        </li>


    <?php endwhile; else: ?>

         </ol>

    <?php endif; ?>

</div>
share|improve this answer
    
why does it cut off my code when i paste it? That's weird. –  MARS Sep 19 '13 at 13:50
    
each line of code must have 4 spaces to format it as a block. the {} button does this for you, click the ? for help and it will explain how to properly format. otherwise this is basically exactly the same as my answer, I just left out the query part, that was implied, perhaps that wasn't clear. one issue with your code though- if ( have_posts() ) should be if ( $the_query->have_posts() ), you're calling that method on a different query without specifically targeting your custom query, it just happens to work in this case. –  Milo Sep 19 '13 at 15:41
    
@Milo Thanks for the tip i haven't gotten around to exploring the "$the_query" in detail yet but it's on my list. –  MARS Sep 19 '13 at 16:40
    
$the_query is just the variable that you've assigned your custom query to, it could be anything, like $cheeseburgers. When interacting with your custom query, you have to reference that query object. If you use the query functions like have_posts() without referencing a query object, they operate on the global $wp_query, which is the default query WordPress runs on every page request. In the case of your example, $wp_query contains the data for the page you're running this custom query on- whatever page you've assigned this Neighborhoods Page template to. –  Milo Sep 19 '13 at 16:49
    
@Milo that makes great sense thanks for tying that together for me This notion of custom queries is neat i'll look into understanding that more. –  MARS Sep 19 '13 at 17:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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