Content and title do not appear on a specific page in Wordpress

I have one "front-page.php" which is a static one side page. If I use the Wordpress loop to view my latest posts on front-page.php, they all display. Now I want to create a news page to create the file "page-news.php". I removed the loop code from the first page and entered it in page-news. Although nothing happens.

Loop code:

<?php get_header();?> <?php if (have_posts()): while (have_posts()): the_post();?> <?php the_title();?> <?php the_content();?> <?php endwhile; else: echo '<p>no posts were found</p>'; endif; ?> <?php get_footer();?> 

What did I miss?

+6
source share
1 answer

you need to add wp_Query on the main page the blog page is considered, so it has a default query.

 $args = array ( /*'cat' => $catNum,*/ 'post_type' => 'post', 'pagination' => false, 'posts_per_page' => '-1', 'ignore_sticky_posts' => false, 'order' => 'DESC', 'orderby' => 'date', ); // The Query $query = new WP_Query( $args ); 

you must add this code before

 if (have_posts()): while (have_posts()): the_post();?> 

this part near have_posts() will be

 // The Loop if ( $query->have_posts() ) { ?> <?php while ( $query->have_posts() ) { $query->the_post(); 

Remember to add wp_reset_postdata(); at the end so you can use many requests on one page.

+2
source

All Articles