[Resolved] archive sticky post

Home Forums Support [Resolved] archive sticky post

Home Forums Support archive sticky post

Viewing 15 posts - 1 through 15 (of 25 total)
  • Author
    Posts
  • #1102090
    Mike

    Hi
    Is there a way to stick a post excerpt to the top of a category archive page?

    Thanks

    #1102236
    David
    Staff
    Customer Support

    Hi there,

    how would you select or decide which excerpt is to be displayed?
    e.g the most recent post in the category.

    #1102264
    Mike

    I have two nav bar choices on a niche site which are ….Reviews and Information. These menu selections each open a category archive page.

    I would like to stick a post to the top of the Reviews category page, so that every time that page opens you see the same review post excerpt at the top of the page with the recent (category review) post following it.

    I’m sure it’s a hair brain idea, but it seems like I’ve been able to do it in the past but can’t make it happen now?

    Anyway, that’s what I would like to do, and whether I can or not isn’t a life or death deal. And I really can’t tell you how great it is to be able to ask you?

    Thank you
    Mike Mahaffey

    #1102286
    David
    Staff
    Customer Support

    Hmmm… had a look at the WP Repo for Stick Posts on Category and all of them are out of date, unsupported or no longer available. Which makes it a more tricky affair.

    Theres a few steps involved:
    1. How to ‘select’ the Sticky Featured Post. I would suggest using a taxonomy Tag for that.
    2. Then we can use that Tag to remove the post from the Category Loop.
    3. And then we could use WP Show Posts and a Hook Element to use the same Tag to display just that post.

    That work for you? If so i can provide the steps to do that.

    #1102302
    Mike

    Thanks David
    I hate I didn’t mention that I had already been through the wp plugins.

    Yes, if you could (at your convenience) get me started with the steps you mentioned I would appreciate it.

    Thanks
    Mike Mahaffey

    #1102323
    David
    Staff
    Customer Support

    OK
    1. Add a new Tag to your featured post. Name it what you like eg. Featured Review.
    2. Add this PHP Snippet to remove the Tag post from the category loop:

    add_filter('pre_get_posts', 'excludeTag');
    function excludeTag($query) {
          if ( $query->is_category('category-slug') ) {
                $query->set('tag', '-1');
          }
          return $query;
    }

    Replace category-slug with the slug of the category eg. reviews
    Replace the -1 in $query->set('tag', '-1'); with the ID of the Tag, you can find that by editing the tag and checking the URL, eg. if the ID=65 then the code will be: $query->set('tag', '-65')

    If that works ie. that post is no longer displayed in the loop then:

    3. Install the WP Show Posts Plugin: https://wordpress.org/plugins/wp-show-posts/
    3.1 Dashboard > WP Show Posts > New. Configure your new list with Taxonomy filter to display just your new Tag and set it to display 1 post and do all the other settings you wish to display.
    3.2 Whilst editing the list it provides you with a shortcode.

    4. Now you can add the Shortcode using a Hook Element:
    https://docs.generatepress.com/article/hooks-element-overview/

    #1940946
    Benjamin

    This is probably a dumb question on two counts, but must I be using GeneratePress as my theme? I do use WP Show Posts with Genesis. next, I tried modifying the above code to a custom taxonomy I’ve created called ‘college’ and have a tag called Seaver Spotlight (ID #2390). But no luck. I get a duplicate: https://newsmigstg.wpengine.com/college/cse/

    add_filter(‘pre_get_posts’, ‘excludeTag’);
    function excludeTag($query) {
    if ( $query->is_college (‘cse’) ) {
    $query->set(‘tag’, ‘2390’);
    }
    return $query;
    }

    #1941491
    David
    Staff
    Customer Support

    Hi there,

    unlike this forum the pre_get_posts filter is not Theme specific 🙂

    For a custom taxonomy you would need to change this condition:

    if ( $query->is_category('category-slug') ) {

    to:

    if ( $query->is_tax('taxonomy','term') ) {

    See her fore more info on is_tax:

    https://developer.wordpress.org/reference/functions/is_tax/

    #1941937
    Benjamin

    That got me pointed in the right direction, thanks a million! This is where I ended up, which is just about where I want to be: https://newsmigstg.wpengine.com/college/cse/

    The function I used is:

    function exclude_posts( $query ) { 
    
    if ( $query->is_tax('college','cse') ) {
    
            $query->set( 'tag__not_in', array( 2390 ) ); 
        } 
    } 
    
    add_action( 'pre_get_posts', 'exclude_posts' );

    An unintended consequence is that this also prevents the post from appearing in the corresponding RSS feed. Any advice on preventing that?

    #1942742
    David
    Staff
    Customer Support

    Try this:

    function exclude_posts( $query ) { 
        if ( is_feed() ) {
            return $query;
        }
        if ( $query->is_tax('college','cse') ) {
            $query->set( 'tag__not_in', array( 2390 ) ); 
        }
        return $query;
    } 
    
    add_action( 'pre_get_posts', 'exclude_posts' );

    This should return no changes if its not the main_query

    #1948430
    Benjamin

    Thanks a million. The excluded item, unfortunately, is AWOl in the feed: http://newsmigstg.wpengine.com/college/cse/feed/

    #1949040
    David
    Staff
    Customer Support
    #1953759
    Benjamin

    Nice!!!

    #1954006
    David
    Staff
    Customer Support
    #1981450
    Benjamin

    Apologies, a follow-up! If I wanted to achieve this same functionality on a standard tag archive page such as this: https://newsmigstg.wpengine.com/tag/lmutw/ would the code be:

    function exclude_posts( $query ) { 
        if ( is_feed() ) {
            return $query;
        }
        if ( $query->set( 'tag__not_in', array( 2414 ) ); {
        return $query;
    } 
    
    add_action( 'pre_get_posts', 'exclude_posts' );
Viewing 15 posts - 1 through 15 (of 25 total)
  • You must be logged in to reply to this topic.