Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Sticky Posts with Latest Post if less then 3 posts are stickied WordPress

  • SOLVED

I want to have some news on my site, however the area I want the news only holds 3 news items, I would like to have it so that If there are only 2 posts sticked it will grab a latest post.

Right now it's grabbing the sticked post but all the same sticky post so it's also not offsetting correctly.

AIM:
1/ Look for sticky posts and display 3.
2/ If only 2 posts are sticked display the 2 PLUS a latest non stickied post.

Here is the code: ( I HAVE THREE OF THESE THE SAME BUT DIFFERENT COUNT NUMBERS - HOWEVER PAGE BREAKS WHEN ENTERING ALL THE CODE)

<!-- start row-->
<?php
$args = array(
'posts_per_page' => 1,
'category_name' => 'news',
'post__in' => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1
);
query_posts( $args );
foreach ($posts as $post) : start_wp(); ?>
<?php static $count1 = 0; if ($count1 == "1") { break; } else { ?>
<div class="row">
<div class="date"><span class="month"><?php the_time('M') ?></span><span class="day"><?php the_time('j') ?></span><span class="year"><?php the_time('Y') ?></span></div>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<p><?php echo $content(12); ?></p>
<a class="more" href="<?php the_permalink(); ?>"></a> </div>
<?php $count1++; } ?>
<?php endforeach; ?>
<!-- / end row -->


PLEASE NOTE: I am not using the same row class for all the rows, they are different the above only showing one, I cannot have one code that showposts=3 as I need to do this seperately because the way the classes are setup on this particlar site.

Things not working:
1/ Offset not working.
2/ Instead of grabbing latest post if less then 3 sticked it deletes the row.

Answers (1)

2011-03-22

AdamGold answers:

When you define 1 post per page, you don't need to make a loop.

<!-- start row-->

<?php

$args = array(

'numberposts' => 3,

'category_name' => 'news',

'post__in' => get_option( 'sticky_posts' ),

'ignore_sticky_posts' => 1

);

$posts = get_posts( $args );

if( count($posts) < 3 ) {

foreach( $posts as $post ) : setup_postdata($post); start_wp(); ?>

<div class="row">

<div class="date"><span class="month"><?php the_time('M') ?></span><span class="day"><?php the_time('j') ?></span><span class="year"><?php the_time('Y') ?></span></div>

<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>

<p><?php echo $content(12); ?></p>

<a class="more" href="<?php the_permalink(); ?>"></a> </div>

<?php endforeach; ?>


<!-- start row-->

<?php

$args = array( 'numberposts' => 1, 'order'=> 'ASC', 'orderby' => 'date' );
$latest_posts = get_posts( $args );

foreach ($latestposts as $post) : setup_postdata($post); ?>
<div>
<?php the_date(); ?>
<br />
<?php the_title(); ?>
<?php the_excerpt(); ?>
</div>
<?php endforeach; ?>


<?php } ?>

<!-- / end row -->



Didn't test it, hope it works.