For our blog, MaxPower, we needed to have certain articles ’stay at the top’ longer than others. We found that the stuff we wrote and really liked was getting bogged down and indistinguishable from the other stuff. For example, on this blog we write about everything, but focus on our travel experiences. The question became, how can we focus our front page to feature these experiances, but not loose all the other stuff we have too? This is what we came up with:

Reading the various discussions on multiple loops from the wordpress help forums gave me an idea of using two loops. One would query and show only posts in one category. The other would query all posts in all categories. The first query was cut and paste from the codex and is easy to understand:


$my_query = new WP_Query('category_name=frontpage&showposts=1');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;

// Do stuff like format the output, maybe in a custom DIV?

In english it translates as, set my_query equal to the result of querying all posts where the catergory is named frontpage and by the way, get me one post only. The key is the last part, set the variable do_not_duplicate equal to the ID number of the single post returned. We will need this value in the next step.

The next piece of code that is important is standard and found in many templates. It simply gets all posts:

if (have_posts()) : while (have_posts()) : the_post();
//do stuff like format each post
endwhile;

In english, this gets all posts, and displays them according to the // do stuff line(s) (template dependant). If you leave the code alone what happens is that your featured post is displayed twice (once in the featured post section, and once in the all other posts section). Obviously this is no good, so this is where the do_not_duplicate variable comes into play. Adding in an if statement prevents the featured post from being displayed twice. Here is that code in context:


if (have_posts()) : while (have_posts()) : the_post();
if( $post->ID == $do_not_duplicate )
continue;
//do stuff like format each post
endwhile;

With the inclusion of the second if statement the code can be translated into english to mean, get all posts, when you find one where the post equals do_not_duplicate then just do nothing (continue), otherwise display all the other the posts according to the // do stuff line(s). Remeber, the do_not_duplicate variable contains the ID of the post already displayed.

So when you put it all together what do you get? A dynamic sticky post! And the best part is once the feature post is replaced with a new one, the previous feature will show up in the catch all post section below (depending on how many posts you choose to display and on the post frequency)

This post has 172 comments.

  1. Root
    23 Apr 05
    8:04 pm

    Very neat expo.

  2. Cyndy
    23 Apr 05
    8:23 pm

    This is great! And you have subsequently solved what has been a niggling problem for me for the last few days! Thank you.

  3. spitfire
    23 Apr 05
    9:50 pm

    I know deepthough. He’s my friend. He’s smart.

    Nice work!

  4. [...] he good stuff you write gets bogged down and indistinguishable from the other stuff. This WordPress hack and tutorial shows you how to focus your front page to [...]

  5. [...] .

    Filed under Design, Code, PHP Help, Wordpress.
    Recently, I showed how to create a dynamic sticky for wordpress. T [...]

  6. Steven S
    19 Aug 05
    1:57 pm

    Just a small fix for your method…

    Running the new query reset’s the page count of the index, so the WP paging mechanism is messed up.

    An easy fix is to add:

    query_posts(’orderby=date’);

    after running ‘The Sticky Loop’. This will reset the post variables, and paging will work again =)

  7. Steven S
    19 Aug 05
    3:02 pm

    Actually, sorry, the about query_posts() command should be:

    query_posts($query_string . ‘&orderby=date’);

  8. Al Z
    10 Nov 05
    1:04 am

    What if you wanted several posts to stay on top, for instance showposts=3?

  9. deepthought
    12 Nov 05
    7:31 pm

    Thats correct, use the showposts option.

  10. Al Z
    15 Nov 05
    8:21 pm

    Yes - the showposts option will work, but this will result in the additional posts duplicating: $post->ID == $do_not_duplicate will work for only one post. Sorry I wasn’t more explicit about the problem.

  11. Al Z
    30 Nov 05
    10:28 pm

    I figured out the solution:

    When using the showpost option for more than one post, e.g. query_posts(’showposts=2′) replace,

    $do_not_duplicate = $post->ID;

    with

    $do_not_duplicate[] = $post->ID;

    This turns the $do_not_duplicate[] into an array that starts numbering the array elements with 0, 1, 2, etc. for each “sticky” post. Then replace,

    if( $post->ID == $do_not_duplicate )

    with

    if( $post->ID == $do_not_duplicate[0] || $post->ID == $do_not_duplicate[1]) etc. adding as many array elements to the conditional statement as there are showposts.

  12. Phillip
    01 Dec 05
    11:31 pm

    ARGH… I cannot get this to work. I keep getting the following error:

    Parse error: parse error, unexpected $ in [removed] on line 95

    …though line 95 is simply ” ?> ” which is the end of my script. I’m obviously doing something wrong…

  13. ste\/e
    10 Dec 05
    12:30 pm

    Al Z,

    you might want to investigate the in_array() function as it would be a generic solution for any number of sticky posts.

    Cheers,

  14. Orin
    24 Jan 06
    7:03 am

    I figured out a way that the sticky itself can be carried over for paging. Enter your query with 'paged='. $paged, like so:

    $your_query = new WP_Query('showposts=1&cat=3&paged='. $paged);

    The loop will then call the most recent post, in that category, for that particular page. I’ve found it very useful — wanted to share.

  15. deepthought
    24 Jan 06
    10:09 am

    Nice moves Orin! Thanks for stopping by.

  16. Lorelle
    03 Sep 06
    11:13 am

    I’ve been looking for a way to showcase a featured post for a long time. This is great.

    I just need to know if the improvements as mentioned in the comments have been added to the article above so it is up-to-date with how the code should be structured? And could you give a specific full example of how to use this to create 1 featured post and then more than one featured post to help make the tutorial easier?

    I’ll be putting this to the test soon. Thanks!

  17. deepthought
    08 Sep 06
    1:01 pm

    I updated this for K2 awhile back, but haven’t looked at it in a long time. So the code hasn’t been updated to reflect people’s helpful comments on this post. I am currently working on another wordpress plugin, after that is done I’ll test out the changes and post an updated version. Based on a quick rescan of everything, it should be fairly straightforward. Feel free to email or contact me for more info.

  18. Amanda
    27 Nov 06
    10:57 am

    Here is my take on this challenge:

    http://velociraptor.info/notes/?p=179

  19. Iwo
    28 Nov 06
    1:42 pm

    Thanks for this fix. I needed this for my website.

  20. Hi Jim. Photos i received. Thanks

  21. ARGH… I cannot get this to work. I keep getting the following error:

    Parse error: parse error, unexpected $ in [removed] on line 95

    …though line 95 is simply ” ?> ” which is the end of my script. I’m obviously doing something wrong…

    I THINK YOU ARE GETTING MY SAME ERROR: THERE IS SOME PROBLEMS WITH “END” TAG

  22. Jude
    23 Jul 07
    7:41 pm

    Good information. I think you should try for more? Seems like a good site.

  23. I unable to implement? Get error? Anyone get errors besides me?

  24. [...] Post en “sticky” dinámica [...]

  25. [...] Post fixo. Há posts que merecem destaque, mas tanto destaque que apenas trocar sua estilo não basta. Então, o que você faz? Deixa ele fixo no início da página. É isso que esse hack faz. [...]

  26. [...] Post fixo. Há posts que merecem destaque, mas tanto destaque que apenas trocar sua estilo não basta. Então, o que você faz? Deixa ele fixo no início da página. É isso que esse hack faz. [...]

  27. [...] Create a Dynamic Sticky (Source: [...]

  28. [...] 9)Creating a “dynamic sticky”- When we need to have certain articles ’stay at the top’ longer than others. [...]

  29. [...] Create a Dynamic Sticky (Source: [...]

  30. Jenny
    25 Mar 08
    1:21 pm

    nifty. now if only i can figure out how to put it all together.

  31. [...] some posts that are important to you, so you ensure they catch the eyes of your readers. Dynamic Sticky Post, Makes the blog to display 2 types of posts. One is the normal, trivial posts. The other type of [...]

  32. prefabrike
    08 Apr 08
    8:47 am

    thank you..

  33. [...] index of posts instead of a paged overview, and by sorting posts by title instead of post date. 9)Creating a “dynamic sticky”- When we need to have certain articles ’stay at the top’ longer than others. 10)Show Category [...]

  34. [...] Theme Hacks. 84. No More CSS Hacks. 85. Create a Dynamic Sticky. 86. Styling Individual Posts Using the_ID. 87. Show Category Images. 88. Separate WordPress [...]

  35. [...] 9)Creating a “dynamic sticky”- When we need to have certain articles ’stay at the top’ longer than others. [...]

  36. [...] WordPress 高级指导 83.WordPress Theme Hacks 84. No More CSS Hacks 85. Create a Dynamic Sticky 86. Styling Individual Posts Using the_ID 87. Show Category Images 88. Separate WordPress Comments [...]

  37. [...] WordPress 高级指导83.WordPress Theme Hacks84. No More CSS Hacks85. Create a Dynamic Sticky86. Styling Individual Posts Using the_ID87. Show Category Images88. Separate WordPress Comments and [...]

  38. [...] Creating a dynamic sticky [...]

  39. [...] Creating a dynamic sticky [...]

  40. [...] 12、文章置顶 [...]

  41. [...] 12、文章置顶 [...]

  42. [...] 12、文章置顶 [...]

  43. [...] 12、文章置顶 [...]

  44. [...] 12、文章置顶 [...]

  45. [...] 12.文章置顶 [...]

  46. [...] 12、文章置顶 [...]

  47. [...] Creating a dynamic sticky [...]

  48. [...] 12、文章置顶 [...]

  49. [...] Wordpress Hack / Tutorial: creating a dynamic sticky [...]

  50. [...] Crea un post “sticky” dinámico [...]

  51. [...] Закрепить новость сверху (как в Datalife) [...]

  52. [...] Una forma rápida y fácil de esconder la publicidad de algunos artículos en particular haciendo unos pequeños cambios en el diseño. 12. Crear un fijador dinámico [...]

  53. [...] Creating a dynamic sticky [...]

  54. [...] Crea un post “sticky” dinámico [...]

  55. [...] 12、文章置顶 [...]

  56. [...] 08. Create a Dynamic Sticky [...]

  57. [...] 08. Create a Dynamic Sticky [...]

  58. [...] 12、文章置顶 [...]

  59. [...] 12、文章置顶 [...]

  60. [...] 12、文章置顶 [...]

  61. [...] 12、文章置顶 [...]

  62. [...] Create a Dynamic Sticky ( Sursa: [...]

  63. [...] Criando um Sticky dinâmico (Fonte: [...]

  64. [...] 12、文章置顶 [...]

  65. [...] Creating a “dynamic sticky” [...]

  66. [...] Creating a dynamic sticky [...]

  67. [...] 12. Creating a dynamic sticky [...]

  68. [...] 原文地址:Wordpress Hack / Tutorial: creating a “dynamic sticky” [...]

  69. [...] 12、文章置顶 [...]

  70. [...] 2. Create a Dynamic Sticky [...]

  71. [...] Creating a dynamic sticky [...]

  72. [...] Create a Dynamic Sticky: (source: maxpower) Category : Front-end [...]

  73. [...] 12、文章置顶 [...]

  74. [...] Создаем топовые новости [...]

  75. [...] Post: url 2. How to Display categories in horizontal Drop-Down menu (source: anthology of ideas) 3. Create a Dynamic Sticky (source: maxpower) 4. How to Add a Print Button To Your Theme (source: hack wordpress) 5. How to [...]

  76. [...] Crea un post “sticky” dinámico [...]

  77. [...] Crea un post “sticky” dinámico [...]

  78. [...] 12、文章置顶 [...]

  79. [...] 9) Create a Dynamic Sticky [...]

  80. [...] 12. Creating a dynamic sticky [...]

  81. [...] Theme Hacks. 84. No More CSS Hacks. 85. Create a Dynamic Sticky. 86. Styling Individual Posts Using the_ID. 87. Show Category Images. 88. Separate WordPress [...]

  82. [...] Creating a dynamic sticky [...]

  83. [...] creating a “dynamic sticky”- Using two loops. One would query and show only posts in one category. The other would query all posts in all categories. [...]

  84. [...] creating a “dynamic sticky”- Using two loops. One would query and show only posts in one category. The other would query all posts in all categories. [...]

  85. [...] creating a “dynamic sticky”- Using two loops. One would query and show only posts in one category. The other would query all posts in all categories. [...]

  86. [...] creating a “dynamic sticky”- Using two loops. One would query and show only posts in one category. The other would query all posts in all categories. [...]

  87. [...] Theme Hacks. 84. No More CSS Hacks. 85. Create a Dynamic Sticky. 86. Styling Individual Posts Using the_ID. 87. Show Category Images. 88. Separate WordPress [...]

  88. [...] 12、文章置顶 [...]

  89. [...] Theme Hacks. 84. No More CSS Hacks. 85. Create a Dynamic Sticky. 86. Styling Individual Posts Using the_ID. 87. Show Category Images. 88. Separate WordPress [...]

  90. [...] Theme Hacks. 84. No More CSS Hacks. 85. Create a Dynamic Sticky. 86. Styling Individual Posts Using the_ID. 87. Show Category Images. 88. Separate WordPress [...]

  91. [...] Create a Dynamic Sticky (Source: [...]

  92. [...] Theme Hacks. 84. No More CSS Hacks. 85. Create a Dynamic Sticky. 86. Styling Individual Posts Using the_ID. 87. Show Category Images. 88. Separate WordPress [...]

  93. [...] Creating a dynamic sticky [...]

  94. [...] Create a Dynamic Sticky (Source: [...]

  95. Lillan
    27 Nov 08
    6:29 pm

    Hi, is it possible for you to publish the code as it should be - the whole mark up with tags?

    Jag want to use this neaty trick as a sideblog with 2 posts but i can’t manage it properly :(

    Thanks for you help in advance.
    //Lillan

  96. [...] 12.报道国内新闻 [...]

  97. [...] 12.报道国内新闻 [...]

  98. [...] 12.报道国内新闻 [...]

  99. [...]  Create a Dynamic Sticky (Source: Maxpower) [...]

  100. [...] WordPress 高级指导 83.WordPress Theme Hacks 84. No More CSS Hacks 85. Create a Dynamic Sticky 86. Styling Individual Posts Using the_ID 87. Show Category Images 88. Separate WordPress Comments [...]

  101. [...] 12.报道国内新闻 [...]

  102. [...] Theme Hacks. 84. No More CSS Hacks. 85. Create a Dynamic Sticky. 86. Styling Individual Posts Using the_ID. 87. Show Category Images. 88. Separate WordPress [...]

  103. [...] Creating a “dynamic sticky” [...]

  104. thx for this, im gonna test it on my new blog, very useful !

  105. [...] Creating a dynamic sticky [...]

  106. [...] Theme Hacks. 84. No More CSS Hacks. 85. Create a Dynamic Sticky. 86. Styling Individual Posts Using the_ID. 87. Show Category Images. 88. Separate WordPress [...]

  107. [...] Create a Dynamic Sticky (Source: [...]

  108. [...] 11.在单篇日志页面屏蔽广告在你的博客主题里做一些小小的修改就能快速简单地屏蔽掉单篇日志中的广告。12.报道国内新闻如果你希望在顶部显示一些新闻的话。13.自定义404错误页面创造你自己的个性404错误页面。14.无论何种主题,统一采用固定的CSS样式无论采用什么主题风格,总能应用某一特定的CSS样式。15.让你的博客像Digg一样对博客设置做出修改,使之有Digg一样的效果。16.将WordPress用作成员列表创建一个显示你博客成员信息的目录列表。 [...]

  109. [...] how to dynamically make posts stay on top longer in category and archive listings using multiple [...]

  110. [...] how to dynamically make posts stay on top longer in category and archive listings using multiple [...]

  111. [...] dynamically make posts stay on top longer [...]

  112. [...] 建立動態置頂消息 有時候你會希望一些特定消息可以貼在網站頂端 [...]

  113. [...] in order to graphically display the date of the published post.29. Dynamic Sticky PagesLearn how to dynamically make posts stay on top longer in category and archive listings using multiple loops.Plugins30. Your First WP PluginMark Jaquith [...]

  114. [...] dynamically make posts stay on top longer [...]

  115. [...] how to dynamically make posts stay on top longer in category and archive listings using multiple [...]

  116. [...] dynamically make posts stay on top longer [...]

  117. [...] how to dynamically make posts stay on top longer in category and archive listings using multiple [...]

  118. [...] Wordpress Hack / Tutorial: creating a dynamic sticky [...]

  119. [...] Create Dynamic Sticky in Wordpress Tagged with: dynamic sticky wordpress [...]

  120. [...] dynamically make posts stay on top longer [...]

  121. [...] 83. Creating a “Dynamic Sticky” Pages [...]

  122. [...] how to dynamically make posts stay on top longer in category and archive listings using multiple [...]

  123. [...] how to dynamically make posts stay on top longer in category and archive listings using multiple [...]

  124. [...] 12. değişken notlar hazırlama [...]

  125. [...] Wordpress Hack / Tutorial: creating a dynamic sticky [...]

  126. [...] که عدم توجه به آن ، مطمئنا نتایج خوبی به همراه ندارد. قرار دادن پستی خاص در بالای دیگر پست ها نمایش تبلیغات فقط به کاربران جستجو کننده صفحه ورود [...]

  127. [...] 12.报道国内新闻 如果你希望在顶部显示一些新闻的话。 13.自定义404错误页面 [...]

  128. [...] قرار دادن پستی خاص در بالای دیگر پست ها [...]

  129. [...] قرار دادن پستی خاص در بالای دیگر پست ها [...]

  130. [...] 83. Creating a “Dynamic Sticky” Pages [...]

  131. John
    24 May 09
    5:06 am

    Hi

    in which file i shall write down this piece of code?
    there are a lot of files….

    kind regards, john

  132. [...] Creating a dynamic stickyThere are some news you just want them to stay on top. [...]

  133. [...] Theme Hacks. 84. No More CSS Hacks. 85. Create a Dynamic Sticky. 86. Styling Individual Posts Using the_ID. 87. Show Category Images. 88. Separate WordPress [...]

  134. [...] Creating a dynamic sticky [...]

  135. [...] 12. Creating a dynamic sticky [...]

  136. [...] Wordpress Hack / Tutorial: creating a “dynamic sticky” [...]

  137. [...]  Wordpress Hack/Tutorial: formulating the “dynamic sticky” [...]

  138. [...] how to dynamically make posts stay on top longer in category and archive listings using multiple [...]

  139. [...] Creating a dynamic sticky [...]

  140. [...] Creating a dynamic sticky [...]

  141. [...]  Wordpress Hack/Tutorial: creating a “dynamic sticky” [...]

  142. [...] how to dynamically make posts stay on top longer in category and archive listings using multiple [...]

  143. [...] Create a Dynamic Sticky (Source: [...]

  144. [...] 3. Wordpress Hack : creating a “dynamic sticky” [...]

  145. [...] Creating a dynamic sticky [...]

  146. [...] Wordpress Hack/Tutorial: creating a “dynamic sticky” [...]

  147. [...] Creating a dynamic sticky [...]

  148. [...] Creating a dynamic sticky [...]

  149. [...] dynamically make posts stay on top longer [...]

  150. [...] dynamically make posts stay on top longer [...]

  151. [...] Creating A Dynamic Sticky [...]

  152. [...] قرار دادن پستی خاص در بالای دیگر پست ها [...]

  153. [...] how to dynamically make posts stay on top longer in category and archive listings using multiple [...]

  154. [...] Creating a dynamic sticky [...]

  155. [...] Creating a dynamic sticky [...]

  156. [...] Creating a dynamic sticky [...]

  157. [...] Creating a dynamic sticky [...]

  158. [...] Creating a dynamic sticky There are some news you just want them to stay on top. [...]

  159. [...] how to dynamically make posts stay on top longer in category and archive listings using multiple [...]

  160. [...] Creating a dynamic sticky [...]

  161. [...] 2. Create a Dynamic Sticky [...]

  162. [...] dynamically make posts stay on top longer [...]

  163. [...] 12.报道国内新闻 [...]

  164. [...] Crea un post “sticky” dinámico [...]

  165. [...] [...]

  166. [...] how to dynamically make posts stay on top longer in category and archive listings using multiple [...]

  167. [...] how to dynamically make posts stay on top longer in category and archive listings using multiple [...]

  168. [...] 3. Wordpress Hack : creating a “dynamic sticky” [...]

  169. [...] Creating a “dynamic sticky” [...]

  170. [...] how to dynamically make posts stay on top longer in category and archive listings using multiple [...]

  171. [...] Create a Dynamic Sticky (Source: [...]

  172. [...] how to dynamically make posts stay on top longer in category and archive listings using multiple [...]