0

I have a custom page template created for my homepage. I have this set to the Static Homepage under the Reading settings. I want to be able to show the last three news posts from my Blog page which is titled News.

I have tried the following loop but it is pulling the content from the custom page content:

<?php
        if ( have_posts() ) :

            if ( is_home() && ! is_front_page() ) : ?>
                <header>
                    <h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
                </header>

            <?php
            endif;

            /* Start the Loop */
            while ( have_posts() ) : the_post();


                get_template_part( 'template-parts/content', get_post_format() );


            endwhile;

            the_posts_navigation();

        else :

            get_template_part( 'template-parts/content', 'none' );

        endif; ?>

I need to show the post title, date, excerpt, and read more link.

I'm also building this off the current version of underscores theme, if that helps you understand why it's calling for the template parts.

Any help would be greatly appreciated.

5
  • What is the differences between your News and posts? is News just a category of posts? And how did you build the News page? What stop you from using part off the News in your front_page design?
    – hcheung
    Commented Mar 28, 2017 at 0:51
  • New is a category of the posts. I did nothing with the News Page besides select it as the Blog Page under the reading settings. I'm not sure what you mean by the last question. Could you please clarify? Edit: I pressed enter before finishing my comment. Did not have the last line in there.
    – Beau Beau
    Commented Mar 28, 2017 at 0:53
  • If News is just a category of the posts, then all you need is to run query_posts() before displaying it. Please take a look at the official documentation on how to query for specific category and number of posts that you were looking for.
    – hcheung
    Commented Mar 28, 2017 at 1:15
  • Thank you. This is all new territory for me. Worked just fine. I don't know if you want to make that an answer so I can say it is answered or not.
    – Beau Beau
    Commented Mar 28, 2017 at 1:32
  • I provided an alternative solution and post it as an answer. You can take a look and decide which way works for you.
    – hcheung
    Commented Mar 28, 2017 at 1:39

1 Answer 1

3

If News is a category of posts, then:

  1. First, find out the category ID, login to wp-admin, go Posts->Categories, mouse over the 'News' category, and cat id should be shown under the left corner of the browser (for chrome this works, not sure about other browsers).
  2. Add the following code into your function.php

    function my_news_category( $query ) {
      if ( $query->is_frontpage() && $query->is_main_query() ) {
        $query->set( 'cat', '18');     //remember change this to the actual cat id
      }
    }
    add_action( 'pre_get_posts', 'my_news_category' );
    

For this solution, you don't have to change the underscore template at all.

Alternate solution would be adding query_posts() before displaying the main loop on your frontage template as I suggested on my comment. See official documentation for more details on how to implement it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.