Instead of showing the latest posts, how to show the latest post from each category on the homepage in...












2














I have the code to display the latest posts on my website, but I wonder if there is a way to make a list of the latest posts, displaying only one post per category. Let's say I have 7 categories, so only 7 posts will be displayed on the page. What should I do?



<?php if ( ! is_single() ) { ?>
<div class="post-container">
<?php } ?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

<?php
//Post Title code

//Post Thumbnail code

//Post Content / Excerpt code

//Post Meta code

</article> <!-- /post -->

<?php if ( ! is_single() ) { ?>
</div>
<?php

<?php } ?>









share|improve this question



























    2














    I have the code to display the latest posts on my website, but I wonder if there is a way to make a list of the latest posts, displaying only one post per category. Let's say I have 7 categories, so only 7 posts will be displayed on the page. What should I do?



    <?php if ( ! is_single() ) { ?>
    <div class="post-container">
    <?php } ?>

    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

    <?php
    //Post Title code

    //Post Thumbnail code

    //Post Content / Excerpt code

    //Post Meta code

    </article> <!-- /post -->

    <?php if ( ! is_single() ) { ?>
    </div>
    <?php

    <?php } ?>









    share|improve this question

























      2












      2








      2







      I have the code to display the latest posts on my website, but I wonder if there is a way to make a list of the latest posts, displaying only one post per category. Let's say I have 7 categories, so only 7 posts will be displayed on the page. What should I do?



      <?php if ( ! is_single() ) { ?>
      <div class="post-container">
      <?php } ?>

      <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

      <?php
      //Post Title code

      //Post Thumbnail code

      //Post Content / Excerpt code

      //Post Meta code

      </article> <!-- /post -->

      <?php if ( ! is_single() ) { ?>
      </div>
      <?php

      <?php } ?>









      share|improve this question













      I have the code to display the latest posts on my website, but I wonder if there is a way to make a list of the latest posts, displaying only one post per category. Let's say I have 7 categories, so only 7 posts will be displayed on the page. What should I do?



      <?php if ( ! is_single() ) { ?>
      <div class="post-container">
      <?php } ?>

      <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

      <?php
      //Post Title code

      //Post Thumbnail code

      //Post Content / Excerpt code

      //Post Meta code

      </article> <!-- /post -->

      <?php if ( ! is_single() ) { ?>
      </div>
      <?php

      <?php } ?>






      wordpress wordpress-theming






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 6:53









      Riya

      134




      134
























          1 Answer
          1






          active

          oldest

          votes


















          1














          It's very easy to add latest post from each category.



          First of all get all the categories of blog by using below code:



          $categories = get_categories();


          Then use foreach ( $categories as $category ) {} to tell WordPress to run through each of these categories in turn and run the code inside the braces.



          Now you need to define the arguments for your query. Inside the braces, add this:



          $args = array(
          'cat' => $category->term_id,
          'post_type' => 'post',
          'posts_per_page' => '1',
          );


          Next, insert your query, using the WP_Query class:



          $query = new WP_Query( $args );

          if ( $query->have_posts() ) { ?>

          <section class="<?php echo $category->name; ?> listing">
          <h2>Latest in <?php echo $category->name; ?>:</h2>

          <?php while ( $query->have_posts() ) {

          $query->the_post();
          ?>

          <article id="post-<?php the_ID(); ?>" <?php post_class( 'category-listing' ); ?>>
          <?php if ( has_post_thumbnail() ) { ?>
          <a href="<?php the_permalink(); ?>">
          <?php the_post_thumbnail( 'thumbnail' ); ?>
          </a>
          <?php } ?>

          <h3 class="entry-title">
          <a href="<?php the_permalink(); ?>">
          <?php the_title(); ?>
          </a>
          </h3>

          <?php the_excerpt( __( 'Continue Reading <span class="meta-nav">&rarr;</span>', 'twentyfourteen' ) ); ?>

          </article>

          <?php } // end while ?>

          </section>

          <?php } // end if

          // Use reset to restore original query.
          wp_reset_postdata();


          This will display each category posts in your home page. Please try to use it and let me know if you have any issue.






          share|improve this answer





















          • man, you're fast... :) Yes this is the right approach for this issue, I can confirm this, since I've used it exactly in the same way. Good answer
            – Angel Deykov
            Nov 23 '18 at 7:34










          • Thanks @Angel Deykov
            – Gajjar Chintan
            Nov 23 '18 at 8:58











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441910%2finstead-of-showing-the-latest-posts-how-to-show-the-latest-post-from-each-categ%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          It's very easy to add latest post from each category.



          First of all get all the categories of blog by using below code:



          $categories = get_categories();


          Then use foreach ( $categories as $category ) {} to tell WordPress to run through each of these categories in turn and run the code inside the braces.



          Now you need to define the arguments for your query. Inside the braces, add this:



          $args = array(
          'cat' => $category->term_id,
          'post_type' => 'post',
          'posts_per_page' => '1',
          );


          Next, insert your query, using the WP_Query class:



          $query = new WP_Query( $args );

          if ( $query->have_posts() ) { ?>

          <section class="<?php echo $category->name; ?> listing">
          <h2>Latest in <?php echo $category->name; ?>:</h2>

          <?php while ( $query->have_posts() ) {

          $query->the_post();
          ?>

          <article id="post-<?php the_ID(); ?>" <?php post_class( 'category-listing' ); ?>>
          <?php if ( has_post_thumbnail() ) { ?>
          <a href="<?php the_permalink(); ?>">
          <?php the_post_thumbnail( 'thumbnail' ); ?>
          </a>
          <?php } ?>

          <h3 class="entry-title">
          <a href="<?php the_permalink(); ?>">
          <?php the_title(); ?>
          </a>
          </h3>

          <?php the_excerpt( __( 'Continue Reading <span class="meta-nav">&rarr;</span>', 'twentyfourteen' ) ); ?>

          </article>

          <?php } // end while ?>

          </section>

          <?php } // end if

          // Use reset to restore original query.
          wp_reset_postdata();


          This will display each category posts in your home page. Please try to use it and let me know if you have any issue.






          share|improve this answer





















          • man, you're fast... :) Yes this is the right approach for this issue, I can confirm this, since I've used it exactly in the same way. Good answer
            – Angel Deykov
            Nov 23 '18 at 7:34










          • Thanks @Angel Deykov
            – Gajjar Chintan
            Nov 23 '18 at 8:58
















          1














          It's very easy to add latest post from each category.



          First of all get all the categories of blog by using below code:



          $categories = get_categories();


          Then use foreach ( $categories as $category ) {} to tell WordPress to run through each of these categories in turn and run the code inside the braces.



          Now you need to define the arguments for your query. Inside the braces, add this:



          $args = array(
          'cat' => $category->term_id,
          'post_type' => 'post',
          'posts_per_page' => '1',
          );


          Next, insert your query, using the WP_Query class:



          $query = new WP_Query( $args );

          if ( $query->have_posts() ) { ?>

          <section class="<?php echo $category->name; ?> listing">
          <h2>Latest in <?php echo $category->name; ?>:</h2>

          <?php while ( $query->have_posts() ) {

          $query->the_post();
          ?>

          <article id="post-<?php the_ID(); ?>" <?php post_class( 'category-listing' ); ?>>
          <?php if ( has_post_thumbnail() ) { ?>
          <a href="<?php the_permalink(); ?>">
          <?php the_post_thumbnail( 'thumbnail' ); ?>
          </a>
          <?php } ?>

          <h3 class="entry-title">
          <a href="<?php the_permalink(); ?>">
          <?php the_title(); ?>
          </a>
          </h3>

          <?php the_excerpt( __( 'Continue Reading <span class="meta-nav">&rarr;</span>', 'twentyfourteen' ) ); ?>

          </article>

          <?php } // end while ?>

          </section>

          <?php } // end if

          // Use reset to restore original query.
          wp_reset_postdata();


          This will display each category posts in your home page. Please try to use it and let me know if you have any issue.






          share|improve this answer





















          • man, you're fast... :) Yes this is the right approach for this issue, I can confirm this, since I've used it exactly in the same way. Good answer
            – Angel Deykov
            Nov 23 '18 at 7:34










          • Thanks @Angel Deykov
            – Gajjar Chintan
            Nov 23 '18 at 8:58














          1












          1








          1






          It's very easy to add latest post from each category.



          First of all get all the categories of blog by using below code:



          $categories = get_categories();


          Then use foreach ( $categories as $category ) {} to tell WordPress to run through each of these categories in turn and run the code inside the braces.



          Now you need to define the arguments for your query. Inside the braces, add this:



          $args = array(
          'cat' => $category->term_id,
          'post_type' => 'post',
          'posts_per_page' => '1',
          );


          Next, insert your query, using the WP_Query class:



          $query = new WP_Query( $args );

          if ( $query->have_posts() ) { ?>

          <section class="<?php echo $category->name; ?> listing">
          <h2>Latest in <?php echo $category->name; ?>:</h2>

          <?php while ( $query->have_posts() ) {

          $query->the_post();
          ?>

          <article id="post-<?php the_ID(); ?>" <?php post_class( 'category-listing' ); ?>>
          <?php if ( has_post_thumbnail() ) { ?>
          <a href="<?php the_permalink(); ?>">
          <?php the_post_thumbnail( 'thumbnail' ); ?>
          </a>
          <?php } ?>

          <h3 class="entry-title">
          <a href="<?php the_permalink(); ?>">
          <?php the_title(); ?>
          </a>
          </h3>

          <?php the_excerpt( __( 'Continue Reading <span class="meta-nav">&rarr;</span>', 'twentyfourteen' ) ); ?>

          </article>

          <?php } // end while ?>

          </section>

          <?php } // end if

          // Use reset to restore original query.
          wp_reset_postdata();


          This will display each category posts in your home page. Please try to use it and let me know if you have any issue.






          share|improve this answer












          It's very easy to add latest post from each category.



          First of all get all the categories of blog by using below code:



          $categories = get_categories();


          Then use foreach ( $categories as $category ) {} to tell WordPress to run through each of these categories in turn and run the code inside the braces.



          Now you need to define the arguments for your query. Inside the braces, add this:



          $args = array(
          'cat' => $category->term_id,
          'post_type' => 'post',
          'posts_per_page' => '1',
          );


          Next, insert your query, using the WP_Query class:



          $query = new WP_Query( $args );

          if ( $query->have_posts() ) { ?>

          <section class="<?php echo $category->name; ?> listing">
          <h2>Latest in <?php echo $category->name; ?>:</h2>

          <?php while ( $query->have_posts() ) {

          $query->the_post();
          ?>

          <article id="post-<?php the_ID(); ?>" <?php post_class( 'category-listing' ); ?>>
          <?php if ( has_post_thumbnail() ) { ?>
          <a href="<?php the_permalink(); ?>">
          <?php the_post_thumbnail( 'thumbnail' ); ?>
          </a>
          <?php } ?>

          <h3 class="entry-title">
          <a href="<?php the_permalink(); ?>">
          <?php the_title(); ?>
          </a>
          </h3>

          <?php the_excerpt( __( 'Continue Reading <span class="meta-nav">&rarr;</span>', 'twentyfourteen' ) ); ?>

          </article>

          <?php } // end while ?>

          </section>

          <?php } // end if

          // Use reset to restore original query.
          wp_reset_postdata();


          This will display each category posts in your home page. Please try to use it and let me know if you have any issue.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 7:25









          Gajjar Chintan

          37118




          37118












          • man, you're fast... :) Yes this is the right approach for this issue, I can confirm this, since I've used it exactly in the same way. Good answer
            – Angel Deykov
            Nov 23 '18 at 7:34










          • Thanks @Angel Deykov
            – Gajjar Chintan
            Nov 23 '18 at 8:58


















          • man, you're fast... :) Yes this is the right approach for this issue, I can confirm this, since I've used it exactly in the same way. Good answer
            – Angel Deykov
            Nov 23 '18 at 7:34










          • Thanks @Angel Deykov
            – Gajjar Chintan
            Nov 23 '18 at 8:58
















          man, you're fast... :) Yes this is the right approach for this issue, I can confirm this, since I've used it exactly in the same way. Good answer
          – Angel Deykov
          Nov 23 '18 at 7:34




          man, you're fast... :) Yes this is the right approach for this issue, I can confirm this, since I've used it exactly in the same way. Good answer
          – Angel Deykov
          Nov 23 '18 at 7:34












          Thanks @Angel Deykov
          – Gajjar Chintan
          Nov 23 '18 at 8:58




          Thanks @Angel Deykov
          – Gajjar Chintan
          Nov 23 '18 at 8:58


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441910%2finstead-of-showing-the-latest-posts-how-to-show-the-latest-post-from-each-categ%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          What visual should I use to simply compare current year value vs last year in Power BI desktop

          Alexandru Averescu

          Trompette piccolo