1

I'm trying to load the 3 latests posts on my homepage. I'm an utter novice but I seem to be making progress.

This is my code (below). At the minute each post has a title of "Home", I understand that is because the homepage is the main query?

<?php
    $latest_blog_posts = new WP_Query( array( 'posts_per_page' => 3 ) );
    if ( $latest_blog_posts->have_posts() ) : while ( $latest_blog_posts->have_posts() ) : $latest_blog_posts->the_post();
        get_template_part('loop');
    endwhile; endif;
?>

So how would I amend this code so it pulls in the 3 latest posts from the blog using loop.php?

I also having a Custom Post Type which uses a different page/loop. But I assume once this is working it would just be a matter of swapping the 'loop' for 'loop-2' to get that working using the same code?

Hope someone can help with this. It's one step forward, two steps back at the minute for me!

EDIT

Contents of loop.php as requested in reply :)

<?php if (have_posts()): while (have_posts()) : the_post(); ?>

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

        <!-- post thumbnail -->
        <?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="h-entry__image-link">
                <?php the_post_thumbnail(array(120,120)); // Declare pixel size you need inside the array ?>
            </a>
        <?php endif; ?>
        <!-- /post thumbnail -->

        <!-- post title -->
        <h2 class="p-name">
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
        </h2>
        <!-- /post title -->

        <!-- post details -->
        <time datetime="<?php the_time('Y-m-j'); ?>" class="dt-published"><?php the_time('jS F Y'); ?></time>
        <!-- /post details -->

        <?php html5wp_summary('html5wp_index'); // Build your custom callback length in functions.php ?>

        <p><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="arrow-link">Read the full article</a></p>

        <?php edit_post_link(); ?>

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

<?php endwhile; ?>

<?php else: ?>

    <!-- article -->
    <article>
        <h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>
    </article>
    <!-- /article -->

<?php endif; ?>
4
  • the output happens via the template file loop.php - so we need to know the full code of that file to be able to say anything...
    – Michael
    Apr 3 '17 at 15:29
  • the problem is that loop.php contains the full code of the default loop, so your custom query before calling the loop.php willl never work. you will possibly need to edit your front-page.php file and add your output code - html and php - instead of get_template_part('loop');
    – Michael
    Apr 3 '17 at 15:57
  • That makes sense and confirms what I feared. Nothings ever simple is it haha! So the code from loop.php would be copied and pasted onto the homepage, so you'd essentially have the same markup in 2 locations? Apr 3 '17 at 17:27
  • I've been told I can use wp_reset_query(); to reset the Wordpress query after looping through a custom query so that it reverts back to the default query for the current page. Does that sound right or will it not work with my loop? Apr 4 '17 at 8:28
4

This is how you would do it. You're loop is based on being in an archive or index page. (or home)

$args = array(
    'posts_per_page'    => 3,
    'post_type'     => 'post',  //choose post type here
    'order' => 'DESC',
);
// query
$the_query = new WP_Query( $args );


if( $the_query->have_posts() ):
    while( $the_query->have_posts() ) : $the_query->the_post();
        get_template_part('loop');
    endwhile; 
else :

endif; 

revised code running full query without the content-part.php

// WP_Query arguments
$args = array(
    'posts_per_page'    => 3,
    'post_type'     => 'post',  //choose post type here
    'order' => 'DESC',
);

// The Query
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
?>
        <!-- article -->
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

    <!-- post thumbnail -->
    <?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="h-entry__image-link">
            <?php the_post_thumbnail(array(120,120)); // Declare pixel size you need inside the array ?>
        </a>
    <?php endif; ?>
    <!-- /post thumbnail -->

    <!-- post title -->
    <h2 class="p-name">
        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    </h2>
    <!-- /post title -->

    <!-- post details -->
    <time datetime="<?php the_time('Y-m-j'); ?>" class="dt-published"><?php the_time('jS F Y'); ?></time>
    <!-- /post details -->

    <?php html5wp_summary('html5wp_index'); // Build your custom callback length in functions.php ?>

    <p><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="arrow-link">Read the full article</a></p>

    <?php edit_post_link(); ?>

</article>
<!-- /article -->
<?php
    }
} else {
    // no posts found
}

// Restore original Post Data
wp_reset_postdata();

i can't fully test it because of the functions being called by your theme, but try it out.

If this does work you you can pull out the article content. Replace all the content in your current loop.php with the content above but only from

<!-- article -->

to

<!-- /article -->

Since that would be in your new loop.php, you'll take it out of the main page.

12
  • Thanks for the reply! I tried this but it just lists 3 blog posts all with the title "Home" and any links just link to the homepage. :/ Apr 3 '17 at 15:56
  • in your dashboard under settings do you have your home page set as blog posts or some other page. you'll want to make sure homepage is not set as blog.
    – rudtek
    Apr 3 '17 at 15:57
  • also just saw your loop. Putting those two files together you are running 2 loops inside each other. That is more likely the problem. Did you create the code on the homepage? Pull the content of the loop out and put on the homepage
    – rudtek
    Apr 3 '17 at 16:02
  • I thought about doing that but then would I need to maintain the loop markup in 2 different locations? Not really a biggie for this project, especially as it's my first dabble into Wordpress. yeah the code is just in front-page.php so I've dropped in the code I need like header/footer and also some Advanced Custom Field markup. Apr 3 '17 at 17:26
  • I've been told I can use wp_reset_query(); to reset the Wordpress query after looping through a custom query so that it reverts back to the default query for the current page. Does that sound right or will it not work with my loop? Apr 4 '17 at 8:27
0

Try this

<?php
    $latest_blog_posts = new WP_Query( array( 'posts_per_page' => 3, 'offset' => 3 ) );
    if ( $latest_blog_posts->have_posts() ) : while ( $latest_blog_posts->have_posts() ) : $latest_blog_posts->the_post();
        get_template_part('loop');
    endwhile; endif;
?>

For more information about WP Query with custom parameters check this link

2
  • How does this answer the OPs question? Offset will show posts 4,5,6 not 'the 3 Latest posts'
    – ngearing
    Apr 5 '17 at 6:13
  • Unfortunately that just creates posts that are titled "Home" and link to the front-page.php as well :/ Apr 5 '17 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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