0

I would really appreciate it if someone could help me with this. I'm trying to get category pages to show only sticky posts from the current category. I was hoping there might be a plugin to do this but it doesn't look like there is. Here's my category template:

 <?php
 /**
* The template for displaying category archive pages.
*
* @link https://codex.wordpress.org/Template_Hierarchy
*
* @package Codilight_Lite
*/

get_header(); ?>
<div id="content" class="site-content container <?php echo codilight_lite_sidebar_position(); ?>">
    <div class="content-inside">
        <div id="primary" class="content-area">
            <main id="main" class="site-main" role="main">

            <?php
            if ( have_posts() ) : $count = 0; ?>

                <header class="page-header">
                    <?php
                        the_archive_title( '<h1 class="page-title">', '</h1>' );
                        the_archive_description( '<div class="taxonomy-description">', '</div>' );
                    ?>
                </header><!-- .page-header -->

                <?php
                $layout_archive_posts = get_theme_mod( 'layout_archive_posts', 'grid' );
                if ( $layout_archive_posts == 'grid' ) {
                    echo '<div class="block1 block1_grid">';
                    echo '<div class="row">';
                        while ( have_posts() ) : the_post();
                        $count++;
                            get_template_part( 'template-parts/content-grid' );
                        if ( $count % 2 == 0 ) {
                            echo '</div>';
                            echo '<div class="row">';
                        }
                        endwhile;
                    echo '</div>';
                    echo '</div>';
                    codilight_lite_custom_paginate();

                } else {
                    echo '<div class="block1 block1_list">';
                        while ( have_posts() ) : the_post();
                        get_template_part( 'template-parts/content-list' );
                        endwhile;
                    codilight_lite_custom_paginate();
                    echo '</div>';
                }
                ?>

            <?php else : ?>

                <?php get_template_part( 'template-parts/content', 'none' ); ?>

            <?php endif; ?>

            </main><!-- #main -->
        </div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
  • have you tried this plugin wordpress.org/plugins/category-sticky-post – bynicolas Aug 10 '16 at 22:40
  • Yes, I did see that plugin but it only allows you to place one post at the top of the archive/category page. I want to ONLY display sticky posts from the current category. I'm using my category pages as landing pages with the category description at the top of the page, and below that I want a "featured" posts section. – jrcollins Aug 10 '16 at 22:58
0

You can try to fetch the sticky posts IDs from the sticky_posts option and then modify the main query on the category archives accordingly:

/**
 * Category Archives: Only display sticky posts for each category term
 */
add_action( 'pre_get_posts', function( \WP_Query $q )
{
    if( ! is_admin() && $q->is_category() && $q->is_main_query() )
    {
        $sticky_posts = get_option( 'sticky_posts' );
        if( ! empty( $sticky_posts ) )
            $q->set( 'post__in', (array) $sticky_posts );
    }
} );

Note that here we're using a simplification by querying for all those sticky posts, regardless of it's categories. We let the taxonomy query of the main query do the filtering instead.

  • Thanks, I tried using the code I found earlier on another thread wordpress.stackexchange.com/questions/165950/…. It works but fetches all sticky posts regardless of what category they're in. Any idea on how to modify it so I'm only getting sticky posts in the current category? – jrcollins Aug 10 '16 at 23:42
  • Did you try my suggestion? @jrcollins – birgire Aug 10 '16 at 23:48
  • Thanks, I'm still working on it I'll post an update here soon. – jrcollins Aug 11 '16 at 0:23
  • ok great, you could paste it into the functions.php file in your current theme directory or better yet create a plugin for it. @jrcollins – birgire Aug 11 '16 at 0:25
  • That works perfectly! I also managed to get it working by modifying the loop on category.php but your's is a better solution. So, you think it's a good idea to make it into a plugin? @birgire – jrcollins Aug 11 '16 at 0:42

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.