admin管理员组文章数量:1323353
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
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?
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/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?
Share Improve this question asked Sep 9, 2020 at 7:58 JacobJacob 11 bronze badge3 Answers
Reset to default 0As 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.
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(); ?>">
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 ) );
本文标签: Having issue with Wordpress loop
版权声明:本文标题:Having issue with Wordpress loop 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742129298a2422084.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论