Support » Developing with WordPress » Help w/ blog: Page 1 (all posts) + Page 2 (latest post only)

  • Resolved samidmatt

    (@samidmatt)


    I am working on creating two pages at the moment:
    1 – One will have all the blog posts.
    2 – The other will ONLY have the latest blog post.

    HOWEVER, I cannot seem to get my blog content on both pages. I have searched the net to see how I could do this, but to no avail. I cannot use categories or plugins to do this, since they want it manually done.

    If I can get some help, I will be using the following code in functions.php (which I believe is correct) so that I can only get the latest post on my other page.

    /**
    Show only latest post on latest blog page!
    */
    function my_main_query($query) {
    
        if( !is_admin() && $query->is_main_query() ) {
            if( is_page( 'singles.php' ) ) {
                $query->set( 'posts_per_page', 1 );
            }
        }
    } add_action('pre_get_posts','my_main_query');
    • This topic was modified 1 year, 10 months ago by samidmatt.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi,

    To display only the latest blog post on a page you’ll need to use a WP Query and The Loop. See here for more information:

    https://codex.wordpress.org/The_Loop
    https://codex.wordpress.org/Class_Reference/WP_Query

    This code would display the most recent blog post with a title and excerpt. You’d place this code in your child theme’s functions.php file. You can read more about what you can include in this snippet in the links I shared above.

    
    // Define our WP Query Parameters
    <?php $the_query = new WP_Query( 'posts_per_page=1' ); ?>
     
    // Start our WP Query
    <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
     
    // Display the Post Title with Hyperlink
    <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
     
    // Display the Post Excerpt
    <li><?php the_excerpt(__('(moreā€¦)')); ?></li>
     
    // Repeat the process and reset once it hits the limit
    <?php 
    endwhile;
    wp_reset_postdata();
    ?>
    

    Hi @mguay22

    Even after I (especially) read the page about the loop, I couldn’t resolve this issue. Even reading those links (and several others like the pre_get_posts), I can’t seem to do one thing – which is what I’m having issues with.

    Basically, I have a custom page that is not the main blog page. And I can’t seem to have any blog post appear on this custom made template page.

    Although, the way I am processing the information (me that is), I can have my main blog page with whatever amount of posts. BUT, I cannot fathom how to do so specifically for a custom made template page. I am basically having issues in terms of controlling my template page.

    • This reply was modified 1 year, 10 months ago by samidmatt.
    • This reply was modified 1 year, 10 months ago by samidmatt.
    Moderator bcworkz

    (@bcworkz)

    I think there’s something missing in your understanding of what’s going on in how WP processes queries and produces web content. It seems you have the right components and are using the right approach, yet it’s not coming together. I’m not sure I can help your understanding, but maybe this post will help.

    mguay22 is correct, the solution lies in WP_Query and the loop. As it should, since nearly all web content produced by WP utilizes these components. As it’s said, the devil is in the details. Or in how you use WP_Query.

    To get other posts to show up on your custom page template, you do not want to use the pre_get_posts action. While this action can be used for any WP_Query, it’s only useful in modifying queries created by other mechanisms. When you make your own WP_Query object, there is little point in using pre_get_posts because you can define the query vars from the onset.

    Recognize that there are basically two major uses of WP_Query. Custom query objects you create and the main query that WP creates based on an HTTP request (the permalink in many cases). In order to use your page template, you create a WP page that utilizes that template. Publishing this page gives you a permalink that’s used to make an HTTP request, which will eventually cause the template code to execute.

    WP knows to use this template because the permalink caused the main query to return the page’s WP_Post object, which contains a reference to your template file. If you were to alter this query through pre_get_posts, the query would likely return something else besides your page, thus your template code will never be executed. To utilize your page template, the main query should not be altered.

    Instead, on your page template, create a new WP_Query object and run a loop to display the returned posts, just as mguay22 suggested. The default page template of any theme should already contain a loop that outputs the result of the main query. In the case of page requests, the main query only returns a single WP_Post object, so this default loop only runs once and outputs the page content and any other post meta the theme decides to output.

    If your custom page is not intended to display content entered on the page edit screen, nor related post meta, you do not even need to run the default loop, it could be removed from the template. Default main query loops are not compatible with loops for custom queries unless the custom query is further manipulated. It’s generally better to construct a proper custom query loop like mguay22 illustrated.

    Is this custom template page the one that should display the one latest post? If so, mguay22’s example should work for you when entered on your custom page template. However, your pre_get_posts code would be inappropriate in this scenario, as it prevents the main query from returning your page, thus your template code would not be used.

    In short, use mguay22’s code on your custom page template and remove your pre_get_posts code. Hopefully all this extra explanation helps you understand why.

    Incidentally, your use of is_page() in your pre_get_posts callback is wrong. It does not take template file names as an argument. It only accepts post ID, slug, or title. It’s for determining if the query is for a specific page. To determine if the query is for any page, use the function without arguments. Since using pre_get_posts is inappropriate in your case, the proper use of is_page() is moot, this is just FYI.

    @bcworkz you are 100% correct, I do not yet have a grasp on this. I am familiar with PHP (in terms of integration), but I don’t have much experience with the backend of WP to be fully capable of understanding how it works. I have quickly input that code into that template page to see the result of doing so, and it worked. I will have to modify the style now though, as I’ll have to make it look like the default blog page – so, I should be good to do that. Initially, I was confused by what mguay was saying, since it referred to the functions.php page, which threw me off completely. I think I might have badly explained what I was speaking about in my first post, hence my confusion after his post. In any event, I truly appreciate your help in explaining what you explained, as well as @mguay22 in providing some tools and code.

    Thanks you, both of you!

    • This reply was modified 1 year, 10 months ago by samidmatt.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Help w/ blog: Page 1 (all posts) + Page 2 (latest post only)’ is closed to new replies.