Hazelek Hazelek - 1 month ago 5
HTML Question

Wordpress loop doesn't work

I created my own template in WordPress, but the loop entries does not work. I would like to entries work on one of the subpages. I also added entries.
This is my code of my subpage. Please help me. I don't know what is wrong. I add a picture under the code.

<?php include 'header.php'; ?>
<main class="subpage-blog">
<div class="subpage-banner">
<div class="container">
<h3>BLOG SIDEBAR</h3>
<div class="breadcrumbs">

</div>
</div>
</div>

<aside class="side-menu col-md-4">
<div class="search">
<h4>Search blog</h4>
<input type="text" value="Search">
</div>
<!-- .search -->
<div class="categories">
<h4>Blog Categories</h4>
<ul class="categories-blog-ul">
<li>Inspirtation</li>
<li>Work</li>
<li>Tech</li>
<li>Creative</li>
</ul>
</div>
<!--.categories-->
<div class="recent-posts">
<h4>Recents posts</h4>
<ul>

</ul>
</div>
<!-- .recent-posts-->
<div class="tags-spot">
<h4>Tags</h4>
<div class="tag"></div>
</div>
<!-- .tags-spot-->
</aside>
<!-- .side-menu-->

<article class="content">
<div class="container">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="news-box">
<div class="news-list-content">
<a href="">
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php the_content('czytaj dalej'); ?>
</a>
</div>
<!-- .news-list-content-->
<div class="image-box-news">
<img src="<?=get_template_directory_uri(); ?>/images/ikona-wpisu.png" alt="" />
</div>
</div>
<!-- .news-box-->
<?php endwhile; else: ?>

<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
</div>
<!-- .container-->
</article>
<!-- .content-->
</main>
<?php include 'footer.php'; ?>


enter image description here

Answer

The problem is that:

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

is using the current page's have_posts query.. That means that it probably will just show whatever that current page template's content would be.

Instead, you'll want to create an entirely new query object and call these functions on it like so:

<?php
  $the_query = new WP_Query( array('posts_per_page' => 10 ) ); //Create our new custom query

  if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?>
   <div class="news-box">
      <div class="news-list-content">
         <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>                      
         <?php the_content('czytaj dalej'); ?>
      </div>

      <!-- all your other markup goes here.... -->

   </div>
<?php endwhile;  else: ?>
      <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
<?php wp_reset_postdata(); //Restore the original postdata for this page, if needed later ?>
Comments