1

Currently I am working for a site which runs on WordPress, and using twenty thirteen theme which already has a sticky post function built in and working reasonably well.

But I do not like how a sticky post is always displayed on absolute top. I would rather be able to display the latest one or two or three posts, then the featured/sticky posts. How can I do this?

  • 2
    If you don't want to display sticky posts on top, why would you use a sticky post? or maybe you can bit more explanatory about what you need exactly – Kumar Dec 20 '13 at 6:32
  • have you tried anything, or you need suggestions on how to do that? – Mark Kaplun Dec 20 '13 at 7:17
  • actually I am creating a home page where I want to display 1 0r 2 regular post on absolute top and the 2 sticky post after them then other regular post after those sticky post. – Suman Gaudel Dec 21 '13 at 8:17
1

You have to change the order in $wp_query->posts. Compare it to the values from get_option( 'sticky_posts' ).

Example:

function move_stickies_down( $num = 1 )
{
    global $wp_query;

    if ( empty ( $wp_query->posts ) )
        return;

    $stickies = get_option( 'sticky_posts' );

    if ( empty ( $stickies ) )
        return;

    $sticky_posts = $top = $after = array();

    foreach ( $wp_query->posts as $p )
    {
        if ( in_array( $p->ID, $stickies ) )
        {
            $sticky_posts[] = $p;
        }
        elseif ( $num > 0 )
        {
            $top[] = $p;
            $num -= 1;
        }
        else
        {
            $after[] = $p;
        }

    }

    $wp_query->posts = array_merge( $top, $sticky_posts, $after );
}

Add this function to your functions.php and call it before the loop like this:

move_stickies_down( 2 );

if ( have_posts() )
{
    while ( have_posts() )
    {
        the_post();
        // show post
    }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.