Posted on 9 February, 2009 By Kim Woodbridge 27 Comments

Using Conditionals with WordPress 2.7 Sticky Posts

Boston Blizzard December 13th 2007 (by Pink Sherbet<br />
Photography)

So, our sticky discussion was interrupted by a couple of love-filled days. But now, I’m back to finish the sticky mess that I started.

To recap: We found the sticky post setting, learned how to add the post class to our templates so the sticky post would actually show up, and then learned how to style the sticky post so it would stand out from a regular post.

Today I am going to discuss WordPress conditionals in relation to sticky posts and how they can make your sticky posts smarter.

Conditionals are if else statements. Parents use them all the time with their children. If you clean your room, you can watch a movie or else you can’t watch a movie. If a condition is met one thing happens. If the condition is not met, something else happens.

In the example I showed with my elephant site, the sticky post did not include the date but a regular post did. Unlike many changes, such as making the background a different color, removing bits of code requires the use of a conditional statement.

eleblog_post
eleblog_sticky

So, on the main index template, I located the code that shows the number of comments and the date at the bottom of each post. It looks like the following:

<p class="to_comments"><span class="date"><?php the_time('F j, Y') ?></span>
  <span class="num_comments">
<?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?>
</span></p>

What I want to do is show all of that on regular posts but remove the date information from sticky posts. So I had an if is sticky else statement around my code. It looks like the following.

<?php
if (is_sticky()) { ;?>

<p class="to_comments">
<?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?></p>

<?php } else { ?>

<p class="to_comments"><span class="date"><?php the_time('F j, Y') ?>
</span>   <span class="num_comments">
<?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?>
</span></p>

<?php } ?>

If you look at the code you can see it was opened with php if is sticky, the next section included the code without the date part of it, then a php else, the full code for regular posts, and then the closing of the php statement. All the little brackets, question marks, colons, etc must be there or you will get errors.

Can you think of other ways you could use the conditional statement? There are tons of things you could do and it is one of the most useful pieces of WordPress code.

photo credit: Pink Sherbet Photography


Related Posts:
  • How to Conditionally Display WordPress Sidebar Content with is_page
  • How to Style a Sticky Post in WordPress 2.7
  • How to Make a Post Sticky in WordPress 2.7
  • (Anti) Social-Lists 5/24/09
  • WordPress Login Security
  • Posted In : WordPress Tips
    If you enjoyed this post, please subscribe to my RSS Feed

    Post to Twitter   Post to Delicious   Post to StumbleUpon

    27 Responses to “Using Conditionals with WordPress 2.7 Sticky Posts”

    Leave a Comment
    You may use: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .


    Recent Comments

      • Dean Dwyer: Ah the power of the comma. It's April, Fools. Who knew punctuation could be so funny. Thanks for the smile....
      • Kim Woodbridge: Hi Christine - Awesome! Ooh ... something shiny ;-)...
      • Kim Woodbridge: Hi - I wish Twitter lists had started sooner. I've worked on them some but I'm having a hard time finding the time to w...
      • Christine: This has been driving me crazy for... maybe forever! I created too many lists and deleting them was not exactly clear -...
      • Ching Ya: Honestly I can't think of a way to live without lists. :-) Twitter lists, Facebook lists.. all of these have helped me a...
      • Kim Woodbridge: Hi Pizza - I guess people have different tastes :-)...
      • Kim Woodbridge: Hi JesC - Great! I'm glad it helped. I had found other tutorials that were hard to follow so I wrote this one up....
      • Kim Woodbridge: Hi Bhagu - Great! I'm glad it helped....