WordPress.org

Ready to get started?Download WordPress

Forums

Remove sticky posts from "posts page" (10 posts)

  1. diagrammar
    Member
    Posted 1 year ago #

    I want to remove sticky posts from my posts page.
    I'm using a child theme of twenty-eleven and the home page (called "Base") uses the showcase template to display featured posts.

    The post page (called "Ticker") uses the sidebar template. I want to remove the featured posts from the top of this page (while retaining them on the showcase template of the home page)

    The site:
    http://www.jrsfantasticmachines.com/

    Thanks!

  2. vtxyzzy
    Member
    Posted 1 year ago #

    If you are using the sidebar-page.php template that comes with T11, make a copy of sidebar-page.php to your child theme's folder and add the following code just before the 'while ( have_posts() )' line:

    <?php
    $stickies = get_option('sticky_posts');
    if( $stickies ) {
       $args = array( 'ignore_sticky_posts' => 1, 'post__not_in' => $stickies );
       global $wp_query;
       query_posts( array_merge($wp_query->query, $args) );
    }
    ?>
  3. diagrammar
    Member
    Posted 1 year ago #

    Thanks, copied sidebar-page.php and added code but no change to the "Ticker" page.

    Do i need to put something into the Child Theme stylesheet that tells it to use sidebar-page.php from the child theme folder?

    The sidebar-page.php in my child theme folder now begins like this:

    get_header(); ?>
    
    		<div id="primary">
    			<div id="content" role="main">
    <?php
    $stickies = get_option('sticky_posts');
    if( $stickies ) {
       $args = array( 'ignore_sticky_posts' => 1, 'post__not_in' => $stickies );
       global $wp_query;
       query_posts( array_merge($wp_query->query, $args) );
    }
    ?>
    
    				<?php while ( have_posts() ) : the_post(); ?>
  4. vtxyzzy
    Member
    Posted 1 year ago #

    No, that should do it. To be sure that you are using the correct template, change the get_header() line to this:

    get_header(); ?><!-- file:sidebar-page.php in child theme -->

    Then view the page, look at the source, and search for file:.

  5. Chip Bennett
    Theme Review Admin
    Posted 1 year ago #

    First: if you're using the static page called "Ticker" for your blog posts index, then that page is not using the sidebar-page.php template file. Per the Template Hierarchy, the Blog Posts index uses, in descending order, only one of the following template files:

    1. home.php
    2. index.php

    So, since Twenty Eleven doesn't include a home.php, the page is actually being rendered using index.php.

    That's really more just FYI, because the solution I propose doesn't care what template file is being used. I would, instead of using query_posts(), filter pre_get_posts instead:

    function mychildtheme_filter_pre_get_posts( $query ) {
        if ( is_page( get_option( 'page_for_posts' ) ) ) {
            $query->set( 'ignore_sticky_posts', true );
        }
        return $query;
    }
    add_filter( 'pre_get_posts', 'mychildtheme_filter_pre_get_posts' );

    Add that to your child Theme's functions.php file. It tells WordPress: "if this page is the page used to display the blog posts index, then modify the query to ignore sticky posts."

    The great benefit of this method (in addition to being, generally, cleaner) is that the query is modified before posts are queried - meaning that everything - including pagination - will work entirely as expected.

  6. Omnilogic
    Member
    Posted 1 year ago #

    Hey Chip,

    Any particular reason you opted to check if it's the page for posts rather than the homepage?

    This works perfect for me.

    function mychildtheme_filter_pre_get_posts( $query ) {
        if ( is_home() ) {
            $query->set( 'ignore_sticky_posts', true );
        }
        return $query;
    }
    add_filter( 'pre_get_posts', 'mychildtheme_filter_pre_get_posts' );
  7. Chip Bennett
    Theme Review Admin
    Posted 1 year ago #

    Any particular reason you opted to check if it's the page for posts rather than the homepage?

    Yes: because I didn't want to assume that you wanted to modify handling of sticky posts if you later decided to display the blog posts index on the site front page.

    As an alternative, you could test for the showcase template itself:

    function mychildtheme_filter_pre_get_posts( $query ) {
        if ( ! is_page_template( 'showcase.php' ) ) {
            $query->set( 'ignore_sticky_posts', true );
        }
        return $query;
    }
    add_filter( 'pre_get_posts', 'mychildtheme_filter_pre_get_posts' );

    That way, you would only ever see/use sticky posts on a page using the Showcase template, regardless of what other template file is loaded.

  8. diagrammar
    Member
    Posted 1 year ago #

    Thanks Chip,

    tried that. it's much simpler. but still no dice.

    The page I call "Ticker" is chosen as the "posts page" in the Reading Settings of WordPress. When I edit the "Ticker" page, it lists the Template as "Sidebar template"

    How would I alter the code you provided to specify the ignore-sticky-posts command for a single page? I want featured (sticky) posts ignored only on the page called "Ticker." They can work normally on all other pages.

  9. diagrammar
    Member
    Posted 1 year ago #

    Thanks done...

    Used Chip's second suggestion and simply tested for sidebar.php rather than showcase.php

  10. awinter
    Member
    Posted 1 year ago #

    Cheers very helpful, was looking for exactly Chip's solution.

Topic Closed

This topic has been closed to new replies.

About this Topic