0

I am learning about WordPress development. I ran into an issue but could not figure out what am I doing wrong

I want to show all of the posts on a page using a custom template. I am following this tutorial https://developer.wordpress.org/themes/basics/the-loop/#blog-archive

So far:

1: I have created a custom page template with the name of "homepage"

2: I have created a page and assigned page template "homepage" and set that page as homepage from the settings panel.

3: In the page template, I have added the below code

/*
 * Template Name: Homepage
 * Template Post Type: page
 */
  
 get_header();  


if ( have_posts() ) : 
    while ( have_posts() ) : the_post(); 
        the_title( '<h2 style="color:#fff;">', '</h2>' ); 
        the_post_thumbnail(); 
        the_excerpt();
    endwhile; 
else: 
    _e( 'Sorry, no posts matched your criteria.', 'textdomain' ); 
endif; 

get_footer();

?>

The above code only displays the current page title and featured image, NOT all of the posts title and featured image.

As per the documentation I am doing everything right, but on the frontend, it is not showing all of the posts, Any idea what I am doing wrong?

3 Answers 3

0

As per the documentation I am doing everything right, but on the frontend, it is not showing all of the posts, Any idea what I am doing wrong?

The documentation does not say to use a custom page template for displaying the blog archive. As shown in the template hierarchy the template for your blog posts archive should be home.php, or index.php. Then in that template you add the loop as described at your link:

<?php 
if ( have_posts() ) : 
    while ( have_posts() ) : the_post(); 
        the_title( '<h2>', '</h2>' ); 
        the_post_thumbnail(); 
        the_excerpt();
    endwhile; 
else: 
    _e( 'Sorry, no posts matched your criteria.', 'textdomain' ); 
endif; 
?>

This template should not have the Template Name: header, or be assigned to a page. Instead it will be applied automatically to whichever page you have selected as the latest posts page in Settings > Reading.

Note that all templates in the template hierarchy use the same basic loop:

while ( have_posts() ) : the_post(); 

endwhile; 

You do not query any posts from the template. WordPress automatically queries the correct posts, and the template's only job is to display whichever posts have been queried. Which template is used, and why, is determined by the template hierarchy, linked above.

0

Please check below code to get all posts

$args = array(
    'posts_per_page' => -1,
    'post_type' => 'post',
    'orderby' => 'date',
    'order' => 'DESC',
);

query_posts($args);
while (have_posts()): the_post();
the_title();
the_excerpt();
get_the_post_thumbnail_url($post->ID);
endwhile;
wp_reset_query();

Please try above code. Note: get_the_post_thumbnail_url(); will return featured image URL. so you can directly add into img tag like

<img src="<?php echo get_the_post_thumbnail_url(); ?>">
1
  • you need to add $args array as per my example. if you put only while loop without args argument then it will return current page data like title,content,image etc.. Commented Sep 9, 2020 at 11:39
-1

When you use a Loop in a specific Page or Post, the loop contains data only from that single Page or Post. So what you are seeing is correct in as much as you have assigned that template to a specific page.

In the case where you want to see more than just the current page or post, you need to create a different kind of loop based on a custom query. There are a few different ways of doing this but if you are new to Wordpress it will really help to start with looking at WP_Query.

When you use WP_Query, you can specify exactly how much and what type of content to collect data for using arguments. Then use the loop to iterate through the results.

Rather than write a code snippet for you, I'd suggest working through the support page on Wordpress as its a good place for grounding your knowledge of WP_Query. You can keep coming back to it.

In particular, see the Argument that specifies the number of posts/pages to retrieve, which if you want all of them, should be set to -1.

$query = new WP_Query( array( 'posts_per_page' => -1 ) );
2
  • Thanks very much for your detailed answer :)
    – Jacob
    Commented Sep 9, 2020 at 8:13
  • Jacob Peatties answer is more correct than mine in as much as if you used the approach of using home.php or archive.php (if you just want an archive page, not a home page) then the Loop acts differently by collecting all posts for you. Knowing which template to use in Wordpress is really good to get a grasp of and will save you time. However, knowing what you can do with WP_query and how it differs from the Loop is also useful if you are new to Wordpress.
    – t2pe
    Commented Sep 9, 2020 at 8:28

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.