Site icon Hip-Hop Website Design and Development

Editing my loop-page.php causes posts to show on all pages

I am working on a project where I needed to list out blog posts when I hit a particular page. Originally I was just using the base code in my loop-page.php file. To do that I was using:

<?php the_content(); ?>

This worked great but then I had to do some customizing when displaying the posts. I haven’t done this before, so I found this code:

$query = new WP_Query(array(
  'post_type' => 'post'
));
                
if ($query->have_posts()) { ?>
  <?php while ($query->have_posts()) {
    $query->the_post(); ?>

    <div class="news-post"> 
      <a href="<?php echo the_permalink() ?>"><p class="news-title"><?php echo get_the_title();?></p></a>
      <p class="news-date"><?php echo get_the_date();?></p>
      <p class="news-excerpt"><?php echo get_the_excerpt();?></p>
    </div>
  <?php
  }
  wp_reset_postdata();
}

This was exactly what I needed for my blog page, but when I went to click to go to my homepage, I still see the same posts layout. I imagine it’s because of the if logic, but I’m not sure where to turn. Is this the proper approach or am I off the mark? Thanks for any input.