Title: Sticky Post Conditional PHP
Last modified: August 19, 2016

---

# Sticky Post Conditional PHP

 *  Resolved [Greg Rickaby](https://wordpress.org/support/users/gregrickaby/)
 * (@gregrickaby)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/sticky-post-conditional-php/)
 * I’m creating an “emergency/breaking news” section above the fold AND outside 
   The Loop.
 * When there is a sticky post, I want to display it (just one) and if there isn’t,
   do nothing. See image example: [Sticky!](http://tinypic.com/r/2mww0e9/6) and 
   [No Sticky](http://tinypic.com/r/20540mf/6)
 * The code below displays a Sticky post just fine, I just need to add the conditional
   tags, therefore I’m lost.
 * Thanks
 *     ```
       <?php
       $sticky = get_option('sticky_posts');
       rsort( $sticky );
       $sticky = array_slice( $sticky, 0, 1);
       query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
       ?>
       <div id="emergency">
           <?php if (have_posts()) ?>
           	<?php while (have_posts()) : the_post(); ?>
   
       	   	<?php images('1', '400', '', 'alignleft', true); ?>
              		<h1>BREAKING NEWS</h1>
              		<h2><?php the_title(); ?></h2>
              		<p><?php excerpt('50'); ?>...<br /><a href="<?php the_permalink(); ?>"><?php echo thesis_teaser_link_text(); ?></a></p>
   
         	<?php endwhile; ?>
       </div>
       ```
   

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/sticky-post-conditional-php/#post-1286731)
 * > I just need to add the conditional tags,
 * You mean if there are no sticky posts?
 *     ```
       <?php
       $sticky = get_option('sticky_possdts');
       if ( ! empty($sticky)) {
       rsort( $sticky );
       $sticky = array_slice( $sticky, 0, 1);
       query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
       ?>
       <div id="emergency">
           <?php if (have_posts()) ?>
           	<?php while (have_posts()) : the_post(); ?>
   
       	   	<?php images('1', '400', '', 'alignleft', true); ?>
              		<h1>BREAKING NEWS</h1>
              		<h2><?php the_title(); ?></h2>
              		<p><?php excerpt('50'); ?>...<br /><a href="<?php the_permalink(); ?>"><?php echo thesis_teaser_link_text(); ?></a></p>
   
         	<?php endwhile; ?>
       </div>
       <?php
       }
       ?>
       ```
   
 *  Thread Starter [Greg Rickaby](https://wordpress.org/support/users/gregrickaby/)
 * (@gregrickaby)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/sticky-post-conditional-php/#post-1286737)
 * Here is the code (with yours included) inside a function I’m building for a Thesis
   Skin.
 * Here is the process:
 * 1. I want this sub-loop to check for stickies (and it does)
    2. If there is a
   sticky, print it 3. If not, do nothing
 *     ```
       <?php
       function tt_emergency_post_html() {
       global $options;
       foreach ($options as $value) {
           if (get_settings( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; } else { $$value['id'] = get_settings( $value['id'] ); }
       }
   
       $sticky = get_option('sticky_possdts');
       if ( ! empty($sticky)) {
       rsort( $sticky );
       $sticky = array_slice( $sticky, 0, 1);
       query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
       ?>
       <div id="emergency">
           <?php if (have_posts()) ?>
           	<?php while (have_posts()) : the_post(); ?>
   
       	   	<?php images('1', '400', '', 'alignleft', true); ?>
              		<h1>BREAKING NEWS</h1>
              		<h2><?php the_title(); ?></h2>
              		<p><?php excerpt('50'); ?>...<br /><a href="<?php the_permalink(); ?>"><?php echo thesis_teaser_link_text(); ?></a></p>
   
         	<?php endwhile; ?>
       </div>
       <?php
       } }
       ```
   
 * With the code above it doesn’t do either…
 * I was trying (and failing miserably):
 *     ```
       <?php
       function tt_emergency_post_html() {
       global $options;
       foreach ($options as $value) {
           if (get_settings( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; } else { $$value['id'] = get_settings( $value['id'] ); }
       }
   
       $sticky = get_option('sticky_posts');
       rsort( $sticky );
       $sticky = array_slice( $sticky, 0, 1);
       query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );?>
   
       	<?php if($sticky >'0') { ?>
       	<div id="emergency">
           		<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
       	   			<?php images('1', '400', '', 'alignleft', true); ?>
              			<h1>BREAKING NEWS</h1>
              			<h2><?php the_title(); ?></h2>
              			<p><?php excerpt('50'); ?>...<br />
               		<a href="<?php the_permalink(); ?>"><?php echo thesis_teaser_link_text(); ?></a></p>
            		<?php endwhile; ?>
   
       	</div>
       <?php else:  ?>
       	NO STICKY!
       <?php }?>
       <?php }
       ```
   
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/sticky-post-conditional-php/#post-1286738)
 * Sorry I messed this up
 *     ```
       $sticky = get_option('sticky_possdts');
       ```
   
 * should be
 *     ```
       $sticky = get_option('sticky_posts');
       ```
   
 *  Thread Starter [Greg Rickaby](https://wordpress.org/support/users/gregrickaby/)
 * (@gregrickaby)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/sticky-post-conditional-php/#post-1286743)
 * Haha! Been there done that! 🙂
 * Thank you, this worked very well.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Sticky Post Conditional PHP’ is closed to new replies.

## Tags

 * [conditional tags](https://wordpress.org/support/topic-tag/conditional-tags/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 2 participants
 * Last reply from: [Greg Rickaby](https://wordpress.org/support/users/gregrickaby/)
 * Last activity: [16 years, 6 months ago](https://wordpress.org/support/topic/sticky-post-conditional-php/#post-1286743)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
