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.

My While loop is breaking the rest of my PHP on the site. Below I put my code that is working but again breaking the rest of the site. Any Suggestions

$query = array( 'post_type' => 'slides', 'orderby' => 'ASC' );
$loop = new WP_Query($query);

while ( $loop->have_posts() ) : $loop->the_post();

        $title = get_the_title();
        $image_object = get_field('slides');
        $alt = $image_object['title'];
        $sizes = $image_object['sizes'];
        $image = $sizes['slider'];

        echo '<div>';
        echo '<div class="slide">';
        echo '<img src="' . $image . '" alt="' . $alt . '" />';
        echo '</div>';
        echo '</div>';


endwhile;

share|improve this question
2  
Do you get an error message? What exactly do you mean by 'broken' can you ellaborate? –  Tom J Nowell Jan 15 '13 at 18:08
1  
ASC is not a valid orderby argument, it's 'order' => 'ASC' –  Milo Jan 15 '13 at 18:10
    
I do not get an error message. What I am doing is running the the function the_field() from ACF and it is not returning any data a string of anything. –  user26062 Jan 15 '13 at 18:22
add comment

1 Answer

A few points:

  • Instead of while(): endwhile; Use while() { }
  • You didn't check to see if any posts were found, if($loop->have_posts()){} will do the trick
  • You didn't cleanup after your query, inside the if statement mentioned above, make a call to wp_reset_postdata(); before your closing brace
  • Avoid generic variables like $query for your args that may already be used elsewhere. $args may be a more appropriate name
share|improve this answer
1  
why would it matter which while syntax is used? –  Milo Jan 15 '13 at 18:09
1  
1: it violates the WP Coding standards, 2: IDEs and editors hate it and won't pick up on it when doing highlighting and auto-indenting, 3: not all server setups have the shorthand turned on, 4: consistency, a lot of plugins and code examples use {} style, and the shorthand while seems almost exclusive to post loops ( copypasta post loops may I add ), and as a result a mishmash of styles is encouraged which is counterproductive –  Tom J Nowell Jan 15 '13 at 18:18
1  
1: it's odd that the default themes violate the coding standards and use this style for all loops, no? 2: depends on the IDE 3: it's not shorthand, it's alternate syntax, it's not something that's enabled/disabled. it really just comes down to preference, you should state it's a matter of opinion and not a reason why the code may be broken. –  Milo Jan 15 '13 at 18:28
    
@Milo Agree that it's not a shorthand. About the Coding "Standards": I guess it's not worth the time discussing this. Maybe you've followed Rarsts "quest" to get a definitive answer on if it's allowed to use file()/file_put_contents()/$wp_filesystem in Themes. About the IDEs) As long as I've used IDEs, there wasn't a single one recognizing end*, extract() or global outside the same file. The ones who don't do this (out of my head) are: Eclipse/Aptana/PHPStorm. Which one do you know about that can do this? –  kaiser Jan 15 '13 at 19:09
    
1: Breaking the rules is not an excuse for others break them 2: I'm not aware of such an IDE but if it exists it's in a minority 3: okay alternate syntax but there are still real pitfalls to using it, sure you can be careful and have clean pretty code with that syntax but that takes more effort –  Tom J Nowell Jan 15 '13 at 20:09
show 4 more comments

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.