简体   繁体   中英

Does $lastBlog = new WP_Query('type=post&posts_per_page=1'); act like an Argument?

I have inserted the folllowing code into my index.php file, in order to generate my Blog Loop on my Homepage:

<?php   
if ( have_posts() ):                        
    while( have_posts() ): the_post();      
?>
<?php 
    endwhile;
endif;              
?>

I have read the WordPress Codex and can see that the below code has been used regularly, instead of my above code. Referring to my Comments, within the below code, is my understanding accurate and correct?

<?php       
    $lastBlog = new WP_Query('type=post&posts_per_page=1'); //This acts like an Argument with an Array.

    if ( $lastBlog->have_posts() ): //The $lastBlog, here, calls/considers the above Argument?
        while( $lastBlog->have_posts() ): $lastBlog->the_post(); 
?>
<?php 
        endwhile;
    endif;              
?>

WP_Query returns an object based on the parameters you feed it. $lastBlog is the variable that object is stored in, and something like $lastBlog->have_posts() is a method of the WP_Query class. So no, it's not an "argument" in the coding sense.

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