Getting started with WordPressget_bloginfo()Enqueuing scriptsMaking network requests with HTTP APIEnqueuing Styleshome_url()Custom Post Typestemplate_includeThe Loop (main WordPress loop)AJAXThe $wpdb ObjectActions and Filterswp_get_current_user()Add/remove contact info for users with user_contactmethods filter hookCreating a custom templateCustomizer Hello WorldCustomizer Basics (Add Panel, Section, Setting, Control)The Admin Bar (aka "The Toolbar")Querying postsAlternating main loop (pre_get_posts filter)ShortcodeCreate a Post Programmaticallyget_template_part()Taxonomiesget_template_part()ShortcodesPost FormatsCustom exerpts with excerpt_length and excerpt_morePlugin developmentSecurity in WordPress - EscapingTemplate hierarchyRemove Version from Wordpress and StylesheetsChild Theme Basicsadd_action()get_template_part()Shortcode with attributeSidebarsSecurity in WordPress - SanitizationinitCreate Template for Custom Post TypeFunction: add_action()Add ShortcodeHow Can I integrate Markdown editor with Advance Custom Field's repeater Add-on.Installation and Configurationwp_get_current_user()WP-CronSecure your installationOptions APIFunction : wp_trim_words()WP_Query() LoopUpdate WordPress ManuallyThemesWP-CLIDebuggingadd_menu_page()add_submenu_page()get_option()get_permalink()get_the_category()the_title()get_the_title()add_editor_style()add_theme_support()WordPress Plugin creationRun WordPress local with XAMPPAdmin Dashboard WidgetsSite MigrationMeta BoxRemove Auto Line Breaks From Content and Excerptget_home_path()Wordpress theme and child-theme developmentREST API

The Loop (main WordPress loop)

Other topics

Basic WordPress loop structure

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;

Alternative loop syntax

You can also use loop with curly brackets like this:

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

        the_post(); 
        var_dump( $post );
    
    }
}

Handling no items in the loop

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;

Contributors

Topic Id: 1803

Example Ids: 5891,5892,5893

This site is not affiliated with any of the contributors.