WordPress

WordPress TutorialRemove Version from WordPress and StylesheetsRun WordPress local with XAMPPSecurity in WordPress - EscapingSecurity in WordPress - SanitizationThe Loop (main WordPress loop)Update WordPress ManuallyWordPress Actions and FiltersWordPress add action()WordPress add editor style()WordPress add menu page()WordPress Add ShortcodeWordPress add submenu page()WordPress add theme support()WordPress Add/remove contact info for users with user contactmethods filter hookWordPress Admin Dashboard WidgetsWordPress AJAXWordPress Alternating main loop (pre get posts filter)WordPress Child Theme BasicsWordPress Create a Post ProgrammaticallyWordPress Create Template for Custom Post TypeWordPress Creating a custom templateWordPress Custom exerpts with excerpt length and excerpt moreWordPress Custom Post TypesWordPress Customizer Basics (Add Panel, Section, Setting, Control)WordPress Customizer Hello WorldWordPress DebuggingWordPress Enqueuing scriptsWordPress Enqueuing StylesWordPress Function : wp trim words()WordPress Function: add action()WordPress get bloginfo()WordPress get home path()WordPress get option()WordPress get permalink()WordPress get template part()WordPress get the category()WordPress get the title()WordPress home url()WordPress How Can I integrate Markdown editor with Advance Custom Fields repeater Add-on.WordPress initWordPress Installation and ConfigurationWordPress Making network requests with HTTP APIWordPress Meta BoxWordPress Options APIWordPress Plugin creationWordPress Plugin developmentWordPress Post FormatsWordPress Querying postsWordPress Remove Auto Line Breaks from Content and ExcerptWordPress REST APIWordPress Secure your installationWordPress ShortcodeWordPress Shortcode with attributeWordPress ShortcodesWordPress SidebarsWordPress Site MigrationWordPress TaxonomiesWordPress Template hierarchyWordPress template includeWordPress the $wpdb ObjectWordPress the Admin Bar (aka The Toolbar)WordPress the title()WordPress theme and child-theme developmentWordPress ThemesWordPress wp get current user()WordPress WP Query() LoopWordPress WP-CLIWordPress WP-Cron



The Loop (main WordPress loop)

From WikiOD

Alternative loop syntax[edit | edit source]

You can also use loop with curly brackets like this:

if ( have_posts() ) {
    while ( have_posts() ) {

        the_post(); 
        var_dump( $post );

    }
}

Basic WordPress loop structure[edit | edit source]

Each time WordPress loads the page, it will run main loop.

The loop is the way to iterate over all elements related to the page you are currently on.

Main loop will work on a global WP_Query object. The query has a globalized method have_posts(), that allows us to loop through all results. Finally inside the loop you can call the_post() method (also as a global function), which sets global post object to the current post inside the loop, and sets the postdata to the current post. Thanks to this you can call functions like the_title, the_content, the_author (template tags) directly inside the loop.

For example if you are on posts lists, main loop will contain a query object with all posts.

If you are on single post (or page), it will contain a query with single post (page) you are currently on.

if ( have_posts() ) : 
    while ( have_posts() ) :
        the_post();
        var_dump( $post );
    endwhile;
endif;

Handling no items in the loop[edit | edit source]

If you want to handle such scenario just add an if/else statement.

if ( have_posts() ) : while ( have_posts() ) : 

    the_post(); 
    var_dump( $post );

endwhile; else :

    __('This Query does not have any results');    

endif;

This article is an extract of the original Stack Overflow Documentation created by contributors and released under CC BY-SA 3.0. This website is not affiliated with Stack Overflow