-
EricJohnsonGuru
Ideally the way I want to do it is dynamic via an element and query loop and not have to make individual category pages like in what i found via searching https://generatepress.com/forums/topic/custom-category-page-with-generate-blocks-or-something-else/#post-2435378.
My thought was that if Query loops can see the current category we could then via tags show featured and popular posts.
Here is an example: https://thenovicechefblog.com/recipes/breakfast/?nowprocket
-
Fernando
Hi Eric,
It will require some custom code.
To clarify, would popular posts equate to “Most viewed posts”? If so, do you already have a plugin or code for this?
Otherwise, if it’s just a tag, let us know.
-
EricJohnsonGuru
My client says:
For each category, I would like to Implement a category landing page with the regular archive stuff at the bottom. Start with a short description of the category, followed by three featured posts from the category and two subcategories. This plugin does it but it is also possible with html coding
https://cultivatewp.com/our-plugins/cultivate-category-pages/ PLUGIN
https://www.spendwithpennies.com/category/recipes/main-dishes/ example of the category landing page.
-
EricJohnsonGuru
Ideally without a plugin and the client can just tag posts they want featured.
-
In that case, use GB’s query loop block, and your client can set up the featured tag, and use the Query loop taxonomy parameter to pull the featured taged posts in the query loop.
And the same goes for the popular posts.
-
EricJohnsonGuru
I have created the query here https://www.loom.com/i/9242977475d341a6a6a6fb89c58bec7a and nothing shows unless I remove the category. Is there a way for the query loop to see which category page it is on and then just apply the featured tag to that query loop?
-
I see, this way should work in the post, but not the archive pages.
Try this method:
1. Add a css class to the grid block of the query loop, eg.
my-css-class
.2. Remove both of the taxonomy parameters from the query loop.
3. Add this PHP code, change
featured
to the actual tag slug.add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) { // Check if it's a category archive page if ( is_category() ) { // Get the current category $current_category = get_queried_object(); // Check if the current category has the 'my-css-class' class if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'my-css-class' ) !== false ) { // Modify tax_query to target the current category and posts with the 'featured' tag $query_args['tax_query'][] = array( 'taxonomy' => 'category', 'field' => 'term_id', 'terms' => $current_category->term_id, 'operator' => 'IN', ); $query_args['tax_query'][] = array( 'taxonomy' => 'post_tag', 'field' => 'slug', 'terms' => 'featured', 'operator' => 'IN', ); // Set the relation to 'AND' $query_args['tax_query']['relation'] = 'AND'; } } return $query_args; }, 10, 2 );
-
EricJohnsonGuru
Wow, using a css class to enable this is clever! Thank you so much this seems to be working!
-
Glad to hear that 🙂
- You must be logged in to reply to this topic.