Posted on 9 February, 2009 By 28 Comments

Using Conditionals with WordPress 2.7 Sticky Posts

 

Boston Blizzard December 13th 2007 (by Pink SherbetPhotography)

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:

Posted In : WordPress Tips

Did you enjoy this article? I can assist you with your WordPress or Facebook project. Contact me and I will provide you with an estimate. You can also connect with me on Facebook or follow me on Twitter.






28 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 badge

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.

Recent Comments

    • Victoria12: Wow! This are so cute. It is so much special when our kids do it on their own and give it us as a surprise gift.. So tou...
    • Catherine: I hope that the page owners have a plan for the transition. One concern is that Facebook doesn’t have a great method of ...
    • harm: You no longer can "Add to My Page" This used to be simple, using the facebook UI. Unfortunately facebook removed thi...
    • Phil Derksen: I was referencing plugin #1 in the list btw. :)...
    • Phil Derksen: Thanks for mentioning my Pin It button plugin. I'd love to hear what people think of it and what they'd like to see adde...

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