This is my first time asking a question on this forum. I hope i can be specific enough. I am trying to replace the front page posts section of a WP template. I have the code below running fine on my index.php page (it gets the posts) when the WP theme is set to settings>reading>your latest posts BUT the way the WP theme "the thinker" is setup reading is set to static page and the posts are gotten through a blog template page (in same location as the index.php file) which gets the posts from loops in separately included files. I'd like to keep it set to static for a few reasons. My question is "Is there a reason why the code below would only work in an index.php file and not out of a blog-template file which is is in the same location as the index.php file. I've checked and the the template-parts called in the code are being called. It seems like there's just no posts to get (which there are).
Thank you for your time,
Dave
<!-- blog content -->
<div class="container">
<div class="row" id="primary">
<main id="content" class="col-sm-8" role="main">
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
get_template_part( 'template-parts/content', get_post_format() );
?>
<?php endwhile; ?>
<?php the_posts_navigation(); ?>
<?php else : ?>
<?php get_template_part( 'template-parts/content', 'none' ); ?>
<?php endif; ?>
</main><!-- content -->
<!-- sidebar -->
<aside class="col-sm-4">
<?php get_sidebar(); ?>
</aside>
</div><!-- primary -->
</div><!-- container -->
have_posts() functions only allowed you to check whether the page have the post or not and as it is your static pages, there will be no posts assign to this page.
You need to query first at the start of the page to show posts. Here is the example.
EDITED::
$args = array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'movie_genre',
'field' => 'slug',
'terms' => array( 'action', 'comedy' ),
),
array(
'taxonomy' => 'actor',
'field' => 'term_id',
'terms' => array( 103, 115, 206 ),
'operator' => 'NOT IN',
),
),
);
$query = new WP_Query( $args );
if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
You can find more about WP_Query in below link.