简体   繁体   中英

Wordpress looping through posts

I wonder if someone can explain the following, I am following a tutorial on wordpress, I see this code being used to loop through the post and display them, however I'm slightly confused as to whats actually happening here.

<?php 
if( have_posts() ):
    while( have_posts() ): the_post(); ?>
        <h3><?php the_title(); ?></h3>
        <p><?php the_content(); ?></p>
    <?php endwhile;     
endif;
?>

The paricular section that concerns me is while( have_posts() ): the_post(); ?> while( have_posts() ): the_post(); ?>

So firstly, the syntax here is not something I have seen before, you have the while loop brackets open and close then after that you have : the post() what is this second part? I though that the condition for the while loop needs to be inside the while() . What is this : the_post() what does it do?

also, the method have_posts() returns true if there are posts, I don't understand why this isn't an infinite loop, since surely have_posts() will always be true as long as there is at least one post in the database.

lastly, I like to use PHP short tags, now when I do, this code no longer works, here is my version of this code with short tags, can someone show me where I am going wrong.

<? if ( have_posts() ): ?>
    <? while ( have_posts() ) : the_post(): ?>
        <h3><?php the_title(); ?></h3>
        <p><?php the_content(); ?></p>
    <? endwhile; ?>
<? endif; ?>

PHP storm is highlighting the last : in <? while ( have_posts() ) : the_post(): ?> as the error, but if I change it to a <? while ( have_posts() ) : the_post(): ?> as the error, but if I change it to a ;` i get the following error

Warning: count(): Parameter must be an array or an object that implements Countable in C:\\laragon\\www\\blog\\wp-includes\\post-template.php on line 293

I realise there are a couple of questions here, I apologise, I just want to fully understand whats actually happening here, not just blindly copy and paste. Hope you understand.

Thanks in advance.

1) While Loop Syntax

The syntax that's being used is an alternative way of doing "control structures" in PHP, as documented here: https://www.php.net/manual/en/control-structures.alternative-syntax.php

Essentially, it's an alternative way of writing the following:

while (have_posts()) {
    the_post();
    ?>
    <h3><?php the_title(); ?></h3>
    <p><?php the_content(); ?></p>
    <?php
}

There's no difference to how the code functions, it's just that it's sometimes easier to match up than using curly braces

2. have_posts() and the_post()

You're right in that this will return true if there are posts, but it's the call to the the_post() function that's stopping it from infinitely looping. What the_post() will do is set all of the variables necessary to access the post data (such as the $post global variable), then it will increase a counter by 1. What this means, if you've only got one post, is that the second call to have_posts will now return false, as the counter is equal to the number of posts it has available.

3. PHP Short Tags

I'm guessing this is because you're server isn't set up to use short tags. You'll need to enable short_open_tag settings within your php.ini config. https://www.php.net/manual/en/ini.core.php#ini.short-open-tag

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM