- This topic has 15 replies, 2 voices, and was last updated 3 years, 3 months ago by
Fernando.
-
AuthorPosts
-
December 30, 2022 at 1:15 am #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
December 30, 2022 at 1:31 am #2477669Fernando 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
19with 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
January 1, 2023 at 7:28 pm #2480101nomadiceman
thank you 🙏
January 1, 2023 at 10:00 pm #2480163Fernando Customer Support
You’re welcome! Happy New Year! 🙂
January 4, 2023 at 8:03 pm #2484128nomadiceman
Happy new year to you too
January 4, 2023 at 8:21 pm #2484136nomadiceman
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.January 4, 2023 at 8:39 pm #2484138Fernando Customer Support
I updated the code above.
Can you try adding it again?
January 4, 2023 at 9:08 pm #2484149nomadiceman
I’ve added it and now the archive disappears
January 4, 2023 at 9:09 pm #2484150nomadiceman
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; } );January 4, 2023 at 9:27 pm #2484162Fernando 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
January 4, 2023 at 9:33 pm #2484166nomadiceman
here you go, thank you
January 4, 2023 at 9:49 pm #2484179Fernando 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.
January 4, 2023 at 10:04 pm #2484182nomadiceman
ive changed it to a tag and changed the id. problem is still there
January 4, 2023 at 10:18 pm #2484185Fernando 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.
January 4, 2023 at 10:22 pm #2484187nomadiceman
thank you. that seems to have worked now
-
AuthorPosts
- You must be logged in to reply to this topic.