Site logo

[Resolved] How to have 3 selected post to be at the top of a category archive page?

Home Forums Support [Resolved] How to have 3 selected post to be at the top of a category archive page?

Home Forums Support How to have 3 selected post to be at the top of a category archive page?

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #2477661
    nomadiceman

    I’m trying to figure out how I can select 3 posts for each category and have them display at the top of the archives for that category. A sort of featured posts for each category.

    Is there a way to do that with a hook and element?

    Are any of the current templates doing that? If so let me know and I will check it out

    Thanks for your help as always

    #2477669
    Fernando
    Customer Support

    Hi Nomadice Man,

    You can use a Query Loop Block. Reference: https://docs.generateblocks.com/article/query-loop-overview/

    Add it through a Block Element – Hook. Hook it to after_header.

    Then, in the Query Loop Parameters, include only posts with Taxonomy – Tag – Featured. (So, you need to tag 3 featured posts as Featured)

    Then, add a PHP snippet to exclude posts with Tag – Featured on your archive pages so it doesn’t repeat.

    Example code:

    add_filter( 'pre_get_posts', function( $query ) {
        if ( $query->is_archive() && $query->is_main_query() ) {
    		$query->set( 'tag', '-19' );
        }
    
        return $query;
    } );

    Replace 19 with the ID of the Tag.

    Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets

    Getting the Tag ID: https://share.getcloudapp.com/Z4umjoAo

    #2480101
    nomadiceman

    thank you 🙏

    #2480163
    Fernando
    Customer Support

    You’re welcome! Happy New Year! 🙂

    #2484128
    nomadiceman

    Happy new year to you too

    #2484136
    nomadiceman

    Hi, I’ve just tried adding the PHP snippet and I got this error:

    Don’t Panic
    The code snippet you are trying to save produced a fatal error on line 6:

    syntax error, unexpected ‘return’ (T_RETURN), expecting ‘)’
    The previous version of the snippet is unchanged, and the rest of this site should be functioning normally as before.

    #2484138
    Fernando
    Customer Support

    I updated the code above.

    Can you try adding it again?

    #2484149
    nomadiceman

    I’ve added it and now the archive disappears

    #2484150
    nomadiceman

    Below is the code I used. The featured category tag id is 113

    add_filter( 'pre_get_posts', function( $query ) {
        if ( $query->is_archive() && $query->is_main_query() ) {
    		$query->set( 'tag', '-113' );
        }
    
        return $query;
    } );
    #2484162
    Fernando
    Customer Support

    Can you provide admin login credentials?

    I’ll check what’s going on and assess what to do.

    Please use the Private Information field for this: https://docs.generatepress.com/article/using-the-premium-support-forum/#private-information

    #2484166
    nomadiceman

    here you go, thank you

    #2484179
    Fernando
    Customer Support

    I see.

    You need to add “Featured” as a tag, not a category.

    Then, add the tag ID in the code.

    Doing so should make it work.

    #2484182
    nomadiceman

    ive changed it to a tag and changed the id. problem is still there

    #2484185
    Fernando
    Customer Support

    I see.

    Can you try changing the code to this?:

    function excludeTagId($query) {
      $tag_Ids = array(
        114
      );
    
      if ($query->is_main_query() && ! is_admin() && $query->is_category() ) {
        $query->set( 'tag__not_in', $tag_Ids );
      }
    }
    add_action('pre_get_posts', 'excludeTagId');

    Let me know how it goes.

    #2484187
    nomadiceman

    thank you. that seems to have worked now

Viewing 15 posts - 1 through 15 (of 16 total)
  • You must be logged in to reply to this topic.