WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Sticky Post Conditional PHP (5 posts)

  1. Greg Rickaby
    Member
    Posted 4 years ago #

    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>
  2. MichaelH
    Member
    Posted 4 years ago #

    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
    }
    ?>
  3. Greg Rickaby
    Member
    Posted 4 years ago #

    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 }
  4. MichaelH
    Member
    Posted 4 years ago #

    Sorry I messed this up

    $sticky = get_option('sticky_possdts');

    should be

    $sticky = get_option('sticky_posts');
  5. Greg Rickaby
    Member
    Posted 4 years ago #

    Haha! Been there done that! :)

    Thank you, this worked very well.

Topic Closed

This topic has been closed to new replies.

About this Topic