0

I want to list titles of the default WP posts of "blog" category in my blog page, which has "Blog" title and uses page-blog.php as template with the following code in it:

<article>
 <?php
   $args = array (
    'post_type' => 'post',
    'category_name' => 'blog'
  );

   $query = new WP_Query( $args );
    if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query-   >the_post();
  ?>
  <h1><?php the_title(); ?></h1>

 <?php
  endwhile;
   wp_reset_postdata();
  else :
 ?>
  <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
  <?php endif; ?>

</article>

No matter what I try it keeps showing all WP posts, regardless to which category they assigned to... even though the same code works perfectly with my custom post types...

Please help! Thanks.

1

Tried your code in a simple page and work correctly. Have you set the "Blog" page as posts page within the "Settings > Reading"?

Because in this case the correct code that the cms will load is home.php or index.php according to the template hierarchy https://developer.wordpress.org/files/2014/10/template-hierarchy.png

  • Thanks for the reply. I didn't set Page for posts in Settings > Reading. Used dynamic front-page.php instead of index.php. I fixed the problem though.... The "blog" category had a child called "news" and that's why those where showing under the "blog" too... Thanks again. P.S. I am an idiot... :) – Will Kanoff Aug 3 '16 at 16:40

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.