• The code below works on the index.php page but not when I put it in a single page template. Does any body know why?

    <?php
    get_header(); // Loads the header.php template
    
    if (have_posts()) : ?>
    
    <div id="blog-wrap" class="blog-wrap-wide blog-isotope clearfix">
    	<?php
    	// Loop through each post
        while (have_posts()) : the_post();
            get_template_part( 'content', get_post_format() );
        endwhile;
        ?>
    </div><!-- /post -->
    
    <?php
    wpex_pagination(); // Paginate your posts
    endif;
    get_footer(); //get template footer ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The while( have_posts() ) : the_post(); etc. etc. is called “The Loop” and will display whichever posts you’ve ‘queried’ based on the page/URL you are visting.

    So on the Blog page it will have queried your 6 latest posts and ‘The Loop’ loops through all six and displays their templates. Whereas something like /about-us is just querying the about us page and showing its template.

    If you want to show your latest posts along side a page, or something else, you’ll need a second query to loop through. Here’s some more information:
    https://codex.www.ads-software.com/The_Loop#Multiple_Loops_in_Action

    Thread Starter robdogrob

    (@robdogrob)

    Thanks Jake.
    So when on a single page, say page-projects.php, it is querying posts only from that page?

    I have looked at the multiple looping options but still don’t understand.
    How do I call a loop that references all posts?
    Why is it different for index vs a page?

    Sorry for being ignorant, thanks for the help!

    I suggest reading that article entirely, but the basic gist is that for every URL there is a ‘Main query’ that handles the main content for that page. So for the blog page the main query gets the latest posts. But on a page template, the main content is that page, so it only queries that page. Any additional content needs to be retrieved manually.

    Perhaps we’re going down the wrong road entirely however. Could you describe exactly what you’re trying to achieve? It’s possible that filtering the main query may be what you need, or even a custom post type.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Calling Blog Posts on Pages’ is closed to new replies.