This code has been updated for K2.
Recently, I showed how to create a dynamic sticky for wordpress. This allows you to keep a post of a certain category at the top of your blog while posts from other categories are ordered as normal below it. You can see it in action here at maxpower. If this is new to you I suggest you read the first post, creating a dynamic sticky for wordpress, for a more indepth explanation of why and what this does.
Right now, I am using a hacked up modified 3 column theme which may have scared away some potential users of this little trick. So here is an example of how it would work with the default Kubrick theme, which seems to be very popular. If you would like to use this functionality all you need due is copy the code shown below over the current contents of your index.php [in .../wp-content/themes/default/]. Note that if you have modified your index.php, you’ll want to include whatever you did in the new file.
Read on for more explanation or skip down to the code itself…
In the initial explanation of how to create a wordpress sticky, I showed the code followed by
//do stuff
. Some people got confused with this, “do stuff.” Do stuff literally means do something, but what that something is, is up to you (do stuff is not a php command, but rather a note to someone viewing the file). In this case, do stuff would mean format each post (add author, show the content, the date etc). For the kubrick theme, the ‘do stuff’ part would be lines 11 through 19 which control how the post is displayed.
Here is the code (note that this 3 column theme doesn’t lend itself to displaying code very well, so at the bottom you can click on the link to view the code as a text file — which is easier to read):
< ?php get_header();?> <div id="content" class="narrowcolumn"> < ?php $my_query = new WP_Query('category_name=Code&showposts=1'); while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID;?> <div class="post_feature"> <h2 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to < ?php the_title(); ?>">< ?php the_title(); ?></a></h2> <small>< ?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small> <div class="entry"> < ?php the_content('Read the rest of this entry »'); ?> </div> <p class="postmetadata">Posted in < ?php the_category(', ') ?> <strong>|</strong> < ?php edit_post_link('Edit','','<strong>|'); ?> < ?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p> <!-- <?php trackback_rdf(); ?> --> </div> < ?php endwhile; ?> < ?php if (have_posts()) : while (have_posts()) : the_post(); if( $post->ID == $do_not_duplicate ) continue;?> <div class="post"> <h2 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to < ?php the_title(); ?>">< ?php the_title(); ?></a></h2> <small>< ?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small> <div class="entry"> < ?php the_content('Read the rest of this entry »'); ?> </div> <p class="postmetadata">Posted in < ?php the_category(', ') ?> <strong>|</strong> < ?php edit_post_link('Edit','','<strong>|'); ?> < ?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p> <!-- <?php trackback_rdf(); ?> --> </div> < ?php endwhile; ?> <div class="navigation"> <div class="alignleft">< ?php posts_nav_link('','','« Previous Entries') ?></div> <div class="alignright">< ?php posts_nav_link('','Next Entries »','') ?></div> </div> < ?php else : ?> <h2 class="center">Not Found</h2> <p class="center">< ?php _e("Sorry, but you are looking for something that isn't here."); ?></p> < ?php include (TEMPLATEPATH . "/searchform.php"); ?> < ?php endif; ?> </div> < ?php get_sidebar(); ?> < ?php get_footer(); ?>
This code has been updated for K2.
08 Aug 05
2:14 pm
Hi!
I’m having a little difficulty with this. I copied the code, and everything works great except….
when I click on the permalink of a post that is below the featured post, it shows the permalink of the featured post with comments above the post that I clicked on. (ie. http://www.popdeal.com/?p=6)
Any ideas?
08 Aug 05
2:32 pm
Sounds like something is messed up. I don’t know what to say, other than to make sure and read up in the CODEX to see the article written there on multiple loops. Or, send me a like with your code in it, not just the php output.
10 Dec 05
10:50 am
Worked very well for me!
I was having trouble implementing this myself but your article clarified it amicably. Half the trouble was not knowing what on earth to call it—I find dynamic sticky is quite concise. Thanks!
11 Dec 05
7:18 pm
Glad someone finds it usefull! Thanks for stopping by.
26 Jan 06
3:05 am
[...] /* For the Sociable Plugin */ .center{ text-align: center; font-size: 1.5em; } img.socialbkmark{ vertical-align: middle; border: 0px; margin-bottom: 4px; } /* End For the Sociable Plugin */ FAQ Q: My favourite social bookmarking site XXXXXX.org.tk.ru is not listed! Can it be listed? A: Absolutely… in order to add your site I need the complete url with the story link and title as template to start off from. This allows me to correctly format the new link. Feel free to contact me. Q: The link to site XXX stopped working! Why? A: From time to time I expect sites like digg and delicious to alter the way they work. You should too. When I become aware of a change on their side I will fix it on mine. You can help by sending me the complete url with the story link and title as template so I can correctly format the new link. Q: Can I change around the order that the sites are listed? A: As of version 0.2 you cannot. I tried to place the sites in order of popularity, but I may be wrong. Q: I noticed that whenever sites are submitted to Fark, they fall into the Misc category. Can I change this? A: Absoutely… although I am just a hack, I did think of this situation. Unfortuneatly, as of V2.0, you must go into the plugin and edit one variable. The default setting is ‘Misc’ — other examples include: Business, Game, News, Politics, Sports, Science/Technology, Website, and Weird News. Examples are shown within the plugin, just use a text editor to make the switch [I recommend PSPad] Q: Are you some kind of PHP guru? A: Absoutely… not! I am a hack at best. One thing I can do well is follow patterns. There are a lot of patterns in PHP. You might be interested in some of the other things I have written for wordpress, namely The Dynamic Sticky and the Sawchuk Scheme for K2 [still in heavy development] Q: Do you have more features planned? A: Absoutely… I am open to suggestions. Drop me a line or leave a comment. Q: Why Sociable? A: A ’sociable’ is analgous to having a party. A sociable is also what one might yell at a party/ bar on the east coast of Canada in order to get all his or her friends to raise their glass, yell “sociable!!!!” and then take a few slugs. You can also do this when being simply around easterners too. Try it with your friends, but do you really need an excuse to bring the glass to your lips? Changelog [...]
12 Feb 06
10:04 pm
Great little hack, deepthought. Got it working well with your suggested code above and a bit of my own CSS to create a Featured Article on my site. Thanx alot.
Best - Michael
27 Mar 06
12:08 pm
[...] Over at the K2 forums, people are diligintly helping other people out. Its a great place to get answers about php coding for your wordpress site and the K2 theme. One of the members of the forum (steveo) wrote up the following code for a dynamic sticky using K2. Originally, I had written up a short tutorial on how to make certain posts ’sticky’, or stay at the top of your blog while other posts lined up behind. This allows you to feature some posts over others. The original code for Kubrick, the default theme for wordpress is here. [...]
06 Apr 06
5:25 pm
I would like to have more than a single “sticky” post held at the top. I tried changing “showposts” to 2 or 3 but the result was I got duplicates, one in the sticky area and one in the “regular” area.
Can you point me in the right direction to figure out how to make a set number of “sticky” posts float on top?
Thanks
10 Apr 06
11:37 am
Hi Anne,
At one time I did know how to do this. Sadly, I guess I didn’t write it down and my brain has since written over the information. Here is what I think needs done:
Set &showposts=X appropriately (2, 3, 40 whatever)
Make $do_not_duplicate an array containing all the ID #’s of the posts you don’t want to show.
Change $post->ID == $do_not_duplicate to something else. The pseudo code would be:
if $post->ID is found within $do_not_duplicate (which is now an array) then do nothing, otherwise do the following…
I hope that gets you going in the right direction
06 Jul 06
5:52 pm
hi folks
my kubrick now has this error
any ideas? how to fix ?
Parse error: parse error, unexpected T_WHILE in /home/marion/public_html/fabradio/wp-content/themes/default/index.php on line 16
—————————————————-
AND THIS IS THE COPIED TEXT I HAVE NAMED ” index.php ” in my Kubrick theme
have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;?>
“>” rel=”bookmark” title=”Permanent Link to “>
Posted in | |’); ?>
–>
ID == $do_not_duplicate )
continue;?>
“>” rel=”bookmark” title=”Permanent Link to “>
Posted in | |’); ?>
–>
Not Found
06 Jul 06
6:13 pm
hi folks:
i have found my error(by the way the index.php text i posted just before this post is not the full text, my mistake)
the problem was that i had included as the first line
PHP:
i should have excluded it.
now there is no error message.
alto
07 Jul 06
5:01 am
hi folks
thankyou for the assistance. the plugin is working very well. thankyou thankyou.
alto
21 Aug 06
11:32 am
Hi,
I used the code above but I am running into the following problem.
With Reading Options set to 1 post, and using only 1 “sticky post” (showposts=1), I can only see 1 post on the front page? On subsequent pages, I see 2 posts: 1 sticky and 1 normal. Is this the intended behaviour?
Thanks!
21 Aug 06
2:53 pm
Louis,
I never messed with the reading options so I’m not sure what it actually does. Its hard to say more without seeing your code and learning what you intend to happen. If you aren’t getting any PHP errors than there is a problem with logic somewhere. Care to share more info?
21 Aug 06
10:15 pm
Hi deepthought,
As I said, I took pretty much the same code which I list below: “upcoming” is the category for the sticky post.
The desired behaviour is that the sticky post is available on index page and all subsequent pages, and I want to show only one entry from the remaining category (set using Reading Options). Using the code below, only the sticky post shows up on the front page and nothing else.
have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID; ?>
“>
ID == $do_not_duplicate ) continue; ?>
“>
21 Aug 06
10:16 pm
Sorry had some problem posting the PHP code. How should I do it by the way?
18 Dec 06
7:31 pm
Cheers for the lesson
15 Feb 07
6:23 pm
[...] If you are looking at the front page of my blog, you will see the layout of the posts is quite different to most other blogs. The home page displays the latest post, then the latest article, then a few more posts. There are a number of posts out there showing how to display a sticky post, even displaying a news post as sticky for only a day or two but none of them quite did everything I wanted them to do. So, after a while looking through the codex article on the loop and scratching my head, I finally got down and worked it out myself, here’s how. [...]
18 Feb 07
7:50 am
[...] Thanks goes to whoever wrote the codex article on the loop and Maxpower and Shaun Andrew’s articles on sticky posts which helped me along brilliantly. [...]
08 Mar 07
3:50 am
whats up
21 May 07
9:33 am
[...] http://www.maxpower.ca/wordpress-hack-sticky-adhesive [...]
19 Aug 07
6:40 am
Does anyone know if this still works for the latest version of Wordpress?
23 Aug 07
12:52 am
It’s mentioned in the top of the article: This code has been updated for K2.
I was googling on “sticky wordpress” and this was exactie what I was looking for to implementate in some wordpress blogs of mine. Thanks!
08 Sep 07
7:01 am
[...] WordPress Hack / Tutorial: Dynamic Sticky Or Adhesive With Kubrick [...]
14 Sep 07
10:15 am
Worked very well for me!
THNX
Billy
22 Sep 07
12:07 am
Worked very well for me!
I was having trouble implementing this myself but your article clarified it amicably. Half the trouble was not knowing what on earth to call it—I find dynamic sticky is quite concise. Thanks!
27 Sep 07
5:08 pm
[...] Sources: This article on multiple loops is a combination of Ryan Boren (http://boren.nu) and Alex King’s (http://www.alexking.org) discussion (http://comox.textdrive.com/pipermail/hackers/2005-January/003578.html) about the Loop on the Hackers Mailing List as well as the tutorial written at MaxPower (http://www.maxpower.ca/wordPress-hack-sticky-adhesive-kubrick/2005/05/03/). [...]
12 Oct 07
8:52 am
[...] This article on multiple loops is a combination of Ryan Boren (http://boren.nu) and Alex King’s (http://www.alexking.org) discussion (http://comox.textdrive.com/pipermail/hackers/2005-January/003578.html) about the Loop on the Hackers Mailing List as well as the tutorial written at MaxPower (http://www.maxpower.ca/wordPress-hack-sticky-adhesive-kubrick/2005/05/03/). [...]
24 Mar 08
1:29 pm
Thanks for nice trick… I’m creating a magazine wordpress theme now and I this seems like good and easy way how to implement featured posts
31 Mar 08
6:14 pm
Hello
Wasjust serfing on net and found this site…want to say thanks. Great site and content!
05 Apr 08
4:31 pm
[...] I’ve been craving for a hack or plug-in that could create a featured article on my blog, a sticky kind of article that would show up on every of my pages, above the rest of all posts and stand out a little - all this without too much hassle. Unfortunately, I couldn’t find a plug-in that would do the dirty work for me, so I had to improvise - after a bit of digging I ended up on Maxpower blog, reading an article wrote by deepthought. [...]
07 Jun 08
5:53 pm
[...] you feel confident playing with coding yourself, you can try this solution, which is meant to work with the popular default Kubrik template, but could probably be tweaked to [...]
11 Jan 09
2:52 am
Hello,
Could you please tell me how to hide a post from the home page only in WP 2.6(7)? In other words, the post should show up under a certain category, but not on the home page.
Thanks.
07 Mar 09
4:54 am
[...] This article on multiple loops is a combination of Ryan Boren and Alex King’s discussion about the Loop on the Hackers Mailing List as well as the tutorial written at MaxPower. [...]
29 Mar 09
8:59 pm
I like the 3 column look. It makes better use of the entire page and allows more stuff to be put on the site.
04 Jun 09
11:13 am
[...] 2: I managed to track down a pretty decent hack rather than a plugin. It comes from deepthought over at MaxPower. It’s a bit more involved to implement compared to most of the wonderful WP plugins, but it seems [...]
28 Jul 09
12:02 am
[...] Website Maxpower Digital fingerprints for content — would this help against plagiarism? Found this interesting – Saved link here for later reference. ALSO; From this Maxpower; wordpress-hack-sticky-adhesive-kubrick [...]
06 Apr 10
12:14 pm
[...] Boren和Alex King 在 Hackers Mailing List 上对循环的讨论以及MaxPower [...]
09 Apr 10
6:29 am
[...] Boren和Alex King 在 Hackers Mailing List 上对循环的讨论以及MaxPower [...]
22 Jul 10
9:32 pm
[...] Boren和Alex King 在 Hackers Mailing List 上对循环的讨论以及MaxPower [...]
29 Jul 10
7:57 am
[...] Boren和Alex King 在 Hackers Mailing List 上对循环的讨论以及MaxPower [...]