How to display Posts in Homepage Using Custom Query in Wordpress

 So first go to front-page.php and find blog area to display posts and use like below code for display posts

  <div class="full-width-split__inner">
      <h2 class="headline headline--small-plus t-center">From Our Blogs</h2>
      <?php
      $homepagePosts = new WP_Query(array(
          'posts_per_page' => 2,    
      ));
     
      while($homepagePosts->have_posts()) {
           $homepagePosts->the_post(); ?>

      <div class="event-summary">
        <a class="event-summary__date event-summary__date--beige t-center" href="<?php the_permalink();  ?>">
          <span class="event-summary__month"><?php the_time('M');  ?></span>
          <span class="event-summary__day"><?php the_time('d');  ?></span>
        </a>
        <div class="event-summary__content">
          <h5 class="event-summary__title headline headline--tiny"><a
              href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
          </h5>
          <p><?php echo wp_trim_words(get_the_content(), 18);  ?> <a href="<?php the_permalink();  ?>"
              class="nu gray">Read more</a></p>
        </div>
      </div>

      <?php }  wp_reset_postdata();
      ?>



      <p class="t-center no-margin"><a href="<?php echo site_url('/blog'); ?>" class="btn btn--yellow">View All Blog Posts</a></p>
    </div>

and save and check.

also add link in top navigation like below 

 <li <?php if(get_post_type() == 'post') echo 'class="current-menu-item"' ?>><a href="#">Blog</a></li>

and save


Comments

Popular posts from this blog

Basics Of PHP

Creating App.js for Node Application

WordPress Full Guide For Developer