PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, April 10, 2022

[FIXED] Display specific post format with limit number of post

 April 10, 2022     format, loops, post, wordpress     No comments   

Issue

I create a loop in Wordpress with the condition like this:

  1. Display a specific post format (example: video)

  2. 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:

  1. I think you have bad wrapping for if else while statements. You are missing an endif at the end in the case your approach is correct.

  2. Why use inside your code the i variable since you can customize any query in WP, especially for number of post using the parameter posts_per_page.

  3. Why not to use the loop inside content-video.php and write only:get_template_part('content-video', get_post_format()); Like in single.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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing