0

I'm working within the Wordpress loop and need to only display a featured image for posts 1, 4, 5, 8, 9, 12, 13 etc (+3+1r) in sequence.

I can set up a counter variable and count the posts but unsure how I can select nth as per the above sequence if anybody can help with the syntax please?

zigojacko
  • 1,983
  • 9
  • 45
  • 77

2 Answers2

0
$posts = get_posts();

to get the last post, either

$last_post = array_pop($posts); //removes it from the array

or

$last_post = end($posts); //sets the internal pointer of $posts to the last element
  • Thanks for your answer but how can I extend `$posts = get_posts();` to say if posts 1, 4, 5, 8, 9 etc in sequence, then display featured image? – zigojacko Jan 23 '15 at 14:32
  • you can use either `WP_Query`, lilke : `$query = new WP_Query( array('post__in' => array( 2, 5, 12, 14, 20 ) ) );` Or, simply create a tag/category for such posts, instead of going through all this! You can refer this wordpress codex, [link](http://codex.wordpress.org/Class_Reference/WP_Query#Parameters) – amitabhdhiwal Jan 23 '15 at 15:06
  • I need to programmatically select the posts not manually type in ID's - that won't work. Nor would creating a tag/category. The styling of the posts on the index page will alternate and I need to display them in a certain way depending on the alternating (i.e - my sequence above). It needs to be done with an `$i=` counter or similar. – zigojacko Jan 23 '15 at 15:18
  • Is this what you are looking for? `$numberOfPosts = 12; $y=1; $z=1; $ids = array(1); for($i=1;$i<$numberOfPosts;$i++){ if($y==1){$y=3;}else{$y=1;} $z = $z+$y; $ids[] = $z; //1,4,5,8,9,13,14 } $query = new WP_Query( array('post__in' => $ids ) );` My bad, the network on my phone doesn't seem to be working, didn't get the notification for your reply. – amitabhdhiwal Jan 23 '15 at 17:35
  • Quite possibly? Is it possible to say 'if posts' do this with it? – zigojacko Jan 23 '15 at 17:39
  • Did you go through the link in my previous comment? The "standard loop" example fits your needs I suppose. `// The Query $the_query = new WP_Query( $args ); // The Loop if ( $the_query->have_posts() ) { echo '
      '; while ( $the_query->have_posts() ) { $the_query->the_post(); echo '
    • ' . get_the_title() . '
    • '; } echo '
    '; } else { // no posts found }`
    – amitabhdhiwal Jan 23 '15 at 17:42
  • I don't think you understand what I'm trying to achieve. I'm not trying to display posts with ID's 1,4,5,8,9,13,14. I'm already working inside the loop and want to display the post differently based on it's ID. – zigojacko Jan 26 '15 at 10:55
  • Somehow it got added as an answer. My bad. – amitabhdhiwal Jan 26 '15 at 12:15
  • Sorry, not the Wordpress post ID, I mean the numerical count value... :) For example, we need to count the posts in the loop (`$i++`) and then say if posts are 1,4,5,8,9,13,14 in loop, do this. – zigojacko Jan 26 '15 at 12:20
  • Ah. Then, simply replace get_the_id() with $i or $counter. – amitabhdhiwal Jan 26 '15 at 12:24
  • But I still haven't determined how to catch the posts in sequence above... I'm stuck with the actual formula that can catch 1,4,5,8,9,13,14 (I don't want to have to manually insert this) - I need something like `$i % 3 == 1` but one that will match my required sequence. – zigojacko Jan 26 '15 at 12:28
  • See [here](http://stackoverflow.com/questions/28150156/catching-iterations-using-php-modulus-in-an-irregular-sequence) – zigojacko Jan 26 '15 at 12:30
  • The for loop in the above comments would generate the sequence. – amitabhdhiwal Jan 26 '15 at 12:37
  • I gave up this way, I couldn't see what you were suggesting I use for what i needed but I have received another answer that works elsewhere now - thanks for all your effort and time. – zigojacko Jan 26 '15 at 13:59
  • 1
    Yeah, just saw your other post. The question was much more clear, and that answer is much better. :) – amitabhdhiwal Jan 26 '15 at 14:01
0

Ah, then simply use the for loop in comments above to get the IDs, and then inside your existing wp loop use if(in_array(get_the_id(),$ids)){//dosomething}