admin管理员组

文章数量:1391987

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
  • just means that the code finds (at least) one post of that cat 367; but there is nothing to output anything of it in your code. try adding the_title() into the loop. – Michael Commented Mar 8, 2020 at 22:58
  • if what you posted is not the full code of your page template, please do post the FULL code. – Michael Commented Mar 8, 2020 at 23:13
  • It's all good, the problem was indeed what you and Jacob below said. I simply forgot to add "the_content();" at the end of the while loop, that's all. Cheers guys! – Joe Commented Mar 8, 2020 at 23:35
Add a comment  | 

1 Answer 1

Reset to default 1

There'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();
}

本文标签: phpDisplay post of specific category on page