Issue
I create a loop in Wordpress with the condition like this:
Display a specific post format (example: video)
Limiting the post number. In this case, I only want to display 2 posts.
Here my code:
<?php $i = 1; if (have_posts()) : while (have_posts() && $i < 3) : the_post(); ?>
<?php get_template_part( 'content-video', get_post_format() ); ?>
<?php $i++; endwhile; ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
I already have file name content-video.php.
Unfortunately, the loop does not work. Only displayed the first post, not a specific post (video post format) with template from content-video.php.
Appreciate for any help, or alternative code. Thanks in advance.
Solution
My remarks about your code:
I think you have bad wrapping for
if else while
statements. You are missing anendif
at the end in the case your approach is correct.Why use inside your code the
i
variable since you can customize any query in WP, especially for number of post using the parameterposts_per_page
.Why not to use the loop inside
content-video.php
and write only:get_template_part('content-video', get_post_format());
Like insingle.php and loop-single.php
the themes provided with wordpress installation (twenty themes) .
Good luck
Answered By - Adib Aroui Answer Checked By - Gilberto Lyons (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.