What's wrong in the code below ? Even after comparing it with pre-existing codes, I don't get the mistake:
$args = array('cat' => 367);
$arr_posts = new WP_Query($args);
if ($arr_posts->have_posts()) {
while ($arr_posts->have_posts()) {
$arr_posts->the_post();
}
} else {
echo "No posts found";
}
Result: Nothing appears, not even the "No posts found" message. But the page is displayed without any error message.
What's wrong in the code below ? Even after comparing it with pre-existing codes, I don't get the mistake:
$args = array('cat' => 367);
$arr_posts = new WP_Query($args);
if ($arr_posts->have_posts()) {
while ($arr_posts->have_posts()) {
$arr_posts->the_post();
}
} else {
echo "No posts found";
}
Result: Nothing appears, not even the "No posts found" message. But the page is displayed without any error message.
Share Improve this question asked Mar 8, 2020 at 22:33 JoeJoe 351 silver badge9 bronze badges 3 |1 Answer
Reset to default 1There's nothing in your code that outputs anything for each post. That's not what $arr_posts->the_post();
does. You need to use template tags like the_title()
and the_content()
to output those fields:
while ($arr_posts->have_posts()) {
$arr_posts->the_post();
the_title( '<h2>', '</h2>' );
the_content();
}
the_title()
into the loop. – Michael Commented Mar 8, 2020 at 22:58