WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Sticky Post Conditional PHP

[Resolved] 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! and No Sticky

    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)
  • 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
    }
    ?>

    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 }

    Sorry I messed this up

    $sticky = get_option('sticky_possdts');

    should be

    $sticky = get_option('sticky_posts');

    Haha! Been there done that! 🙂

    Thank you, this worked very well.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Resolved] Sticky Post Conditional PHP’ is closed to new replies.