-1

I was readind this article (https://codex.wordpress.org/Class_Reference/WP_Query) and try run the Standard Loop, but I can't receive the any result. My code is this:

<?php get_header();
/*
    Template Name: Page teste
*/
?>
    <?php
    echo "ola";
    // The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();

    get_footer();
?>

This code is on file call "page-test.php". Which is a template for a page on my WordPress.

  • 1
    where does $args come from? – Milo Jan 23 '16 at 22:31
  • Did you receive the "ola" message? – Nathan Powell Jan 24 '16 at 0:38
  • 1
    You don't have any query arguments. – s_ha_dum Jan 24 '16 at 2:43
0

Yes you need some arguments defined.

A good place to easily get your loops defined is: https://generatewp.com/wp_query/

Here's an example of some arguments used.

// WP_Query arguments
$args = array (
    'post_type'              => array( 'post' ),
    'nopaging'               => true,
    'posts_per_page'         => '3',
    'ignore_sticky_posts'    => true,
);

// The Query
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        // do something
    }
} else {
    // no posts found
}

// Restore original Post Data
wp_reset_postdata();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.