admin管理员组文章数量:1289565
I am creating a theme for my site. I would like to display the last 5 articles on the home page but with a different display for the first articles. So I created this code that I try to tweak but impossible to display the first article with the first template and the 4 others with the second template. I don't know if it's possible to do it the way I want to do it, but this way seems to be the easiest
My code :
<?php
$query = [
'post_type' => 'post',
'posts_per_page' => 5
];
$query = new WP_Query($query);
while ($query->have_posts()) : {
$query->the_post();
if($query === 1) :
get_template_part('loops/acc-cards');
else() :
get_template_part('loops/cards');
}
wp_reset_postdata();
?>
I am creating a theme for my site. I would like to display the last 5 articles on the home page but with a different display for the first articles. So I created this code that I try to tweak but impossible to display the first article with the first template and the 4 others with the second template. I don't know if it's possible to do it the way I want to do it, but this way seems to be the easiest
My code :
<?php
$query = [
'post_type' => 'post',
'posts_per_page' => 5
];
$query = new WP_Query($query);
while ($query->have_posts()) : {
$query->the_post();
if($query === 1) :
get_template_part('loops/acc-cards');
else() :
get_template_part('loops/cards');
}
wp_reset_postdata();
?>
Share
Improve this question
edited Jul 15, 2021 at 13:32
Outkax
asked Jul 13, 2021 at 17:46
OutkaxOutkax
14 bronze badges
3 Answers
Reset to default 0I haven't tested this, but it looks like you just need to set an iteration variable (usually abbreviated $i
), like this:
<?php
$i = 0; // This sets your variable
$query = [
'post_type' => 'post',
'posts_per_page' => 5
];
$query = new WP_Query($query);
while ($query->have_posts()) : {
$query->the_post();
if($i == 0) :
get_template_part('loops/acc-cards');
else() :
get_template_part('loops/cards');
}
$i++ // This iterates the iteration variable $i
wp_reset_postdata();
?>
Solution found, it missed an endwhile;
<?php
$query = array(
'post_type' => 'post',
'posts_per_page' => 5
);
$query = new WP_Query($query);
while ($query->have_posts()) :
$query->the_post();
if($query->current_post === 0) :
get_template_part('mining-inc/loops/acc-cards');
else :
get_template_part('loops/cards');
endif;
endwhile;
wp_reset_postdata();
?>
The proper day to do it in WordPress is to use the current_post
value instead of setting your own variable like $i
. current_post
is the index of the current post in a loop that starts with 0.
<?php
$query = array(
'post_type' => 'post',
'posts_per_page' => 5
);
$query = new WP_Query($query);
while ($query->have_posts()) :
$query->the_post();
if($query->current_post === 0) :
get_template_part('loops/acc-cards');
else:
get_template_part('loops/cards');
endif;
endwhile;
wp_reset_postdata();
?>
本文标签: phpDisplay articles with a different template in the home pageSolved
版权声明:本文标题:php - Display articles with a different template in the home page | Solved | 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741438744a2378791.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论