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.
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
Jim says
Thanks ,Kim. This looks like it could be really useful. Though, If…statements give me bad flashbacks to three college semesters of C++. ;-)
Jim´s last blog post – Bavaria Filmplatz, Here I Come!
Kim Woodbridge says
Jim – LOL, 3 semesters? I don’t remember that far back so I can’t comment on what I did or didn’t study in college.
Jim says
It was technically 3, 10 week terms. That didn’t make it any less painful. ;-) I guarantee that I would do better in the courses if i took them right now though.
Jim´s last blog post – Bavaria Filmplatz, Here I Come!
Kim Woodbridge says
True that – I remember the 10 weeks terms of info cramming
;-)
Ajith Edassery says
Hey Kim,
off topic question. What style have you used for the pre tag. I never got it to work…still using sreenshots for code and then separate downloadable for actual code module :(
Cheers,
Ajith
Ajith Edassery´s last blog post – SEO – Link Building Series: The importance of Linking
Kim Woodbridge says
Hi Ajith – I use a plugin called Synax Highlighter. When you use the pre code have you tried replacing each opening > with “& l t ;” ? (no double quote and no spaces) I have to do that even with the plugin but the plugin allows me to put code within a blockquote.
Ajith Edassery says
I tried various styles, they never worked. Right now, I m using a space between the ‘<‘ and the tag sometimes… :lol:
Will try the plugin, though I don’t have too much of code samples in my posts
Kim Woodbridge says
It’s worked pretty well for me. I don’t use code too frequently – when I do I always have to go back to an earlier post to remind myself how I did the code …
Vered - MomGrind says
Honestly? I’m not too crazy about sticky posts. :) I feel part of a blog’s attraction is fresh content. But I do see how this could be useful if there’s something you do want to keep at the top.
Vered – MomGrind´s last blog post – Sex Sells? You Tell Me
Kim Woodbridge says
Hi Vered – Just because something can be done doesn’t mean it should be done ;-)
Nihar says
Kim,
One more good tutorial of sticky post series. I have used conditional tags on single.php a lot.
I would like to add on what ajith has asked. I use syntax highlighter and use & l t ; for < and so on…
But after every time i update the post. these characters keep doubling. like if i have one lt spl char. After one more updatation of post, i get two and so on.
To avoid this what i do is like ajith is :( I have copied that code part on my PC and paste it everytime i edit the post.
Any solution to this?
Nihar´s last blog post – Microsoft Vista Answers – Find solution to Windows Vista problems
Kim Woodbridge says
Hi Nihar – I’m really not sure – I’ve never had that happen. Do you
use the visual or html editor? I use the html – maybe that’s why
I don’t have the same problems.
Sounds like I should do an article on adding code samples to posts.
;-)
Palma | Buddha Trance says
Kim,
This is very helpful information! I haven’t used a sticky post yet, but I can foresee a possible use in the future. I never thought about the idea of using conditional tags and styling them.
Thank you for pointing it out!
Palma | Buddha Trance´s last blog post – 68 seconds of Pure Thought
Kim Woodbridge says
Hi Palma – Thanks for stopping by and commenting! I’m glad you think the article is useful.
Bryan says
Kim
Your articles about removing the date from a sticky post are the best I have found on the internet. However, the code you supplied is useless to dangerous in the hands of someone like me who doesn’t know hardly anything about coding. That’s not your fault, and neither are the peculiarities of the theme I am using on my WordPress site (I use a really neat theme called Mandigo).
Luckily I found a couple of really helpful guys on the internet, Wizzer and Randy the Hermit, who told me exactly what to do . I had to open up my functions.php file and find this part of the code around line 165:
// this function builds the ‘date stamps’
function mandigo_date_stamp($date) {
global $mandigo_options;
I put a space below the last line and then added the following:
//stop dates on stickies
if (is_sticky()) return;
Maybe it was magic, but it worked for me and my theme. I sure am glad to have this working now and you can see it in action at http://www.adventurestories.info
I’d also like to give props to Randy the Hermit and Wizzer who were seriously amazing. Their websites are:
http://howtoblogsite.com/
http://wizzersays.com/
Thanks for the inspiration
Bryan
Bryan´s last blog post – Find the Articles You Want
Kim Woodbridge says
Hi Bryan – Thank you for visiting and commenting.
I’m glad you found a solution when mine did not work for you.
Do you know why it didn’t work? Is it an issue related to the theme
that you are using.
I will check out Randy and Wizzer’s sites.
Jayme says
Oh my goodness…that was probably the scariest thing I’ve done in WordPress to date, but your instructions made it easy! Thanks so very much for this tutorial. You rock!
Jayme´s last blog post – Lesson #2
Kim Woodbridge says
Hi Jayme – Thank you for visiting and commenting. And thanks for the compliment!
It can be very scary when you first start messing around with the code. Just make a backup of the file you are working on and you can always put everything back the way it was if it gets messed up.
I visited your site – the theme is lovely. I love it. Are you using the sticky post code for the ‘What’s New’ section?
Jayme says
Yes. I used your code to take out all title and date/time info, then put the title “What’s new?”back into the post itself after some line breaks (because the title was overlapping with the header in an ugly way). Works perfectly! I’m using the same technique on my personal blog.
I did make a copy of the ‘original’ before messing around with it, just in case. Back up, back up, back up!
I disagree with the commenter who felt that modifying the main index template is ‘dangerous’. I find it’s far more dangerous to modify code in the core files of WordPress. But whatever works!
Thanks again for your help :-)
Jayme´s last blog post – Lesson #2
Kim Woodbridge says
Hi Jayme – I think he meant dangerous for someone
who doesn’t know what they are doing because they will
mess up the files but maybe I misread that. :-)
Tenny says
Thank you for the clear and easy to follow tutorial. This helped me accomplish exactly what I was trying to do!
Kim Woodbridge says
Hi Tenny – Thanks! I’m glad it worked out for you.
Trisha says
holy crap i did it! :) every time i think i really must learn php, some lovely person comes along to teach me exactly what I need to know. This is great, still working in 2.9 as well. THANKS!
.-= Trisha´s last blog ..Different Styles by Category =-.
Kim Woodbridge says
Hi Trisha – Thanks! That’s great. I hadn’t tested this in 2.9 – I guess I was assuming it would still work. :-)
Trisha says
Im glad it does! Im building a free theme, trying to learn php by doing. Your code made it really look nice. Check it out (gave you a shout out too) http://drlab.digitalrenewal.com/SEOtheme/
Finally got the ‘get first image’ code working. Man that was tough. Hopefully with 2.9 thumbnails it will be easier.
I wondered, if you can totally redesign a sticky post, can you redesign the most recent post in a similar fashion? I looked in the codex and searched google but couldnt figure it out. Have you ever done it?
I love your portfolio btw!
.-= Trisha´s last blog ..Different Styles by Category =-.
Kim Woodbridge says
Hi Trisha – Thanks!
The first post can be styled differently. Here are a couple of articles I located on the subject.
http://perishablepress.com/press/2009/12/01/stupid-wordpress-tricks/#swt_14
http://adambrown.info/b/widgets/easy-php-tutorial-for-wordpress-users/first-post-different-from-the-rest/
http://jameswhittaker.com/journal/8-top-tips-for-wordpress-theme-development/
Trisha says
you are a saint! James Whittakers code was right on the money for my skill level. that one is super versatile! the only thing I did to get it working on an index page, was to remove the category (etc) from query_posts… and now I can make killer category feeds. such good stuff. THANK YOU!!!! I still like your sticky code for the SEO theme, but this is giving me other ideas. too…many…projects :)
.-= Trisha´s last blog ..CSS Positioning =-.
Dave from The Longest Way Home says
Small suggestion for some of the great tips you have here. Maybe the series plugin?
Or just inserting some links to tie them all together. Or even YARRP.
Had to search a while to link all you sticky posts together :)
Great tips and a walk through.