Posted on 9 February, 2009 By Kim Woodbridge 27 Comments

Using Conditionals with WordPress 2.7 Sticky Posts

 
Share

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. You can also connect with my Facebook page or follow me on Twitter.



    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> .

    CommentLuv Enabled

    Recent Comments

      • Ari Herzog: Agreed on the updates/inbox snafu. I rarely see page updates and I know where they are! .-= Ari Herzog´s last blog .....
      • jayson: Kim, Love the themes. I am looking to update the theme on my site to something more appropriate. My site is all abou...
      • vered: "I wish more people would use this. I think it’s silly when I get updates for things occuring in Kansas and California –...
      • Dennis Edell @ Direct Sales Marketing: I was speaking generally, not specifically this month. how exactly do you work things? .-= Dennis Edell @ Direct Sales M...
      • Lucy Beer: Unfortunately I think next to no-one actually sees those "Updates". They're buried in a subsection of the inbox and I do...
      • Kim Woodbridge: Hi Marbella - Thanks! I had a great time....
      • Kim Woodbridge: Hi Julie - I'll probably try kayaking again next summer but I don't know if I will become much of a kayaker - I had a di...
      • Kim Woodbridge: Hi Vered - If you ever get the chance, you should go. It's like a different world - it's so quiet and peaceful....

    About

    Kim Woodbridge is an accomplished Information and Technical Consultant specializing in the entire implementation of a WordPress based website including installation, theme design, upgrades, unique customizations and ongoing site maintenance.

    Wordpress Services

    • Installation, upgrades and maintenance
    • Conversion of existing html and css templates
    • Theme and plugin recommendations
    • CSS customizations
    • Troubleshooting and tweaks for unique situations
    • Customization for individual blogging goals and needs
    • Training and advice