Question

How to stick multiple posts in a single category to show on top of that category?

Tried Ideas: I tried to use "Category Sticky Post" plugin, but it only allows me to pin one post for each category.

Also tried to stick posts to the homepage, and remove the featured posts from the home page, but they don't stick on top of the category.

Was it helpful?

Solution

Your category.php should have two loops — one for stickies, one for regular posts:

<?php
// get sticky posts array
$sticky = get_option('sticky_posts');

// First WP_Query arguments
$args = array(
    // include stickies
    'post__in'  => $sticky
);

$query = new WP_Query( $args );

// First loop for stickies only
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post() );

    echo get_the_title() . '<br />';
    echo get_the_content() . '<br />';

endwhile; endif;

wp_reset_postdata(); // don't forget to reset before the next loop

/***************************************/

// Second WP_Query arguments
$args = array(
    // exclude stickies
    'post__not_in' => $sticky
);

$query = new WP_Query( $args );

// Second loop for posts excluding stickies
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post() );

    echo get_the_title() . '<br />';
    echo get_the_content() . '<br />';

endwhile; endif;

wp_reset_postdata(); // if necessary

More than likely, you will need a lot of additional WP_Query parameters.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top