• Hi all,

    I’m having a little bit of difficulty with a theme I’m designing. Basically, the front page of my site (test version here: http://nouse.pling.org.uk/) is using query_posts to display the latest posts in each category. It is my understand that sticky posts should be displayed at the top by default.

    For example, in the “Politics” section on that site, the article “Ourselves to blame: Clegg on the EU” is sticky, but appears in its natural (by date) order, with a non-sticky post top.

    How do I get sticky posts to appear top? Here’s the code for that box:

    <div class="frontpagesection" id="politics">
      <h2>Politics</h2>
    <?php
        query_posts('category_name=politics&showposts=4');
        the_post();
    ?>
      <div>
        <h3><a href="<?php the_permalink(); ?>"><img src="<?php the_article_photo(0); ?>" /><?php the_title(); ?></a></h3>
        <?php the_excerpt(); ?>
    
      </div>
      <ul class="links">
    <?php
        while (have_posts())
        {
          the_post();
    ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php
        }
    ?>    <li class="more"><a href="<?php bloginfo('url'); ?>/politics/">More Politics</a></li>
      </ul>
    </div>

    Any advice would be great, thanks 🙂

Viewing 10 replies - 1 through 10 (of 10 total)
  • I thought the sticky feature was for front page only?

    Thread Starter chrisnorthwood

    (@chrisnorthwood)

    According to the documentation: http://codex.wordpress.org/Template_Tags/query_posts#Sticky_Post_Parameters

    “Posts that are set as Sticky will be displayed before other posts in a query, unless excluded with the caller_get_posts=1 parameter.”

    It doesn’t mention anything about being specific about which page it’s been called on, unless the Codex is wrong.

    great – hope someone knows more about this

    Thread Starter chrisnorthwood

    (@chrisnorthwood)

    Okay, done a little bit of playing around, stickies work when a category isn’t specified. It appears as if you can’t get stickies for particular categories, how annoying.

    Thread Starter chrisnorthwood

    (@chrisnorthwood)

    Okay, if anyone else comes across this, I’ve written some code that implements what I want:

    function get_posts_with_stickies($cat, $num)
    {
        global $post;
        $finalposts = array();
        $stickiesID = array();
        // append relevant stickies to the front
        query_posts(array('post__in'=>get_option('sticky_posts')));
        while (have_posts())
        {
            the_post();
            if (in_category($cat) && $post->post_status == 'publish')
            {
                $finalposts[] = $post;
                $stickiesID[] = $post->ID;
            }
        }
        // if any of the stickies are in what we've got, remove them
        $args = array(
            'category' => $cat,
            'numberposts' => $num
        );
        $posts = get_posts($args);
        foreach ($posts as $post)
        {
            if (!in_array($post->ID, $stickiesID))
            {
                $finalposts[] = $post;
            }
        }
        // check we've not exceeeded the max length
        while (count($finalposts) > $num)
        {
            array_pop($finalposts);
        }
        return $finalposts;
    }

    Basically, pop this in your theme’s functions.php, and then call get_post_with_stickies with the first argument as your category ID, and the second as the number of posts to return, and it will return a list of posts, with anything stickied in that category at the front.

    thanks – appreciate you coming back with this

    hey chris, thanks for the code, but for some reason it doesn’t work for me!

    I stuck in the function in my functions.php, and called out in a specific category page this:
    get_posts_with_stickies(9,3)

    What happens after that is that my category page (in this instance category-3.php) only has one post – the sticky post, but not the rest.

    Am I doing something wrong?

    Thanks in advance!

    Also I forgot to mention that that category is a child category, just in case it affects it.

    Try using this plugin. Its just what I needed and solved my problem.

    http://wordpress.org/extend/plugins/astickypostorderer/

    Thread Starter chrisnorthwood

    (@chrisnorthwood)

    Hi morticya33 – sorry, didn’t realise you’d posted in this thread.

    Actually, the code I’ve posted above does have this bug. Here’s the fixed version which I used (sorry for not updating it on here)

    function get_posts_with_stickies($cat, $num)
    {
        global $post;
        $finalposts = array();
        $stickiesID = array();
        // append relevant stickies to the front
        if (count(get_option('sticky_posts')) > 0)
        {
            query_posts(array('post__in' => get_option('sticky_posts')));
            $iscomma = strpos($cat, ',');
            if ($iscomma === false)
            {
                $maincat = $cat;
            }
            else
            {
                $maincat = substr($cat, 0, $iscomma);
            }
            while (have_posts())
            {
                the_post();
                if (in_category($maincat) &amp;&amp; $post->post_status == 'publish')
                {
                    $finalposts[] = $post;
                    $stickiesID[] = $post->ID;
                }
            }
        }
        // if any of the stickies are in what we've got, remove them
        $args = array(
            'cat' => $cat,
            'showposts' => $num,
            'caller_get_posts' => 1
        );
        query_posts($args);
        while (have_posts())
        {
            the_post();
            if (!in_array(get_the_ID(), $stickiesID))
            {
                $finalposts[] = $post;
            }
        }
        // check we've not exceeeded the max length
        while (count($finalposts) > $num)
        {
            array_pop($finalposts);
        }
        return $finalposts;
    }
Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Sticky posts not working?’ is closed to new replies.