Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Query Loop Featured Posts in Category Archive Page Hero Element

  • I am working to replace multiple WP Show Posts instances with Query Loop blocks.

    The place I need help with at the moment is category archive pages. The top section of each category archive is created by a Page Hero Block Element. The featured image, title and category description are all dynamic elements.

    We were pulling in a list of six highlighted posts (selected by applying a tag) using a shortcode to create an WP Show Posts instance. I removed that when I began setting up the Query Loop block.

    When the Query Loop block didn’t pull in any posts, I searched and found Ying’s code on this page:

    The OP there seemed to be trying to solve the same problem, but probably without the whole page hero element.

    I added the PHP to a code snippet. I removed the parameters I had added when I first set up the Query Loop block. I changed the PHP to match the tag name (Highlights) and CSS class I added. I do see content in the block on the editing screen, but it doesn’t actually display anything on the front end.

    Should this be working regardless of the fact that it’s inside an Element? Do I need to do anything differently?

  • Hi there,

    The grandparent container of the query loop has the hide class which hides the container responding to your CSS:

    .hide {
        display: none;

  • πŸ€¦πŸ½β€β™€οΈ

    Oh. My. Gawd.

    Thank you so much, Ying. For this, and for the original post.

    I added that class when I wanted to try a Query Loop on the live site, and didn’t want it to show on the front. Completely forgot about it when I copied the site over.

    Thanks again πŸ™‚

  • No worries, all good πŸ™‚

    More than happy to help!

  • I was so happy this was working that I didn’t think to test the load more posts button. The client discovered over the weekend that clicking the Show More button loads post in the wrong container, below the Query Loop block and above the main content area on the page where it would normally be appended to the bottom.

    You can see the behavior on any category page in the staging site I shared credentials for in my original request. You can also see it on the one category page on the live site that has the Query Loop block instead of the WP Show Posts instance.

    I hope I’ve just fat fingered something, but nothing is jumping out at me so far. Thanks for any assistance you can provide.

    Here’s the code snippet I’m using:

    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 'highlights-grid' class
            if ( ! empty( $attributes['className'] ) &&
                 strpos( $attributes['className'], 'highlights-grid' ) !== false ) {
                // Modify tax_query to target the current category and posts with the 'Highlights' 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'    => 'Highlights',
                    'operator' => 'IN',
                // Set the relation to 'AND'
                $query_args['tax_query']['relation'] = 'AND';
        return $query_args;
    }, 10, 2 );
  • Hi there,

    There isn’t a link to your staging site.

    However, the infinite scroll function does not support query loop block at this moment.

    If it’s for archives, you will have to use block element – content template instead of using block element – loop template, just like what you’ve done for the production site.

  • I’m sorry – I assumed the private info I originally shared would persist, but forgot about the fact that it would be deleted after I marked the topic as resolved. I’ve shared the credentials again.

    I am trying to use the Query Loop only for highlighted posts – not the main category post feed. We don’t want infinite scroll or load more with these Query Loop block posts. Only the main post feed.

    On the live site, we are still using the WP Show Posts blocks on all categories except the one I mentioned in my last message. We aren’t using the content template block at all β€” just letting the default category page template handle everything but the hero section.

  • You can see the behavior on any category page in the staging site I shared credentials for in my original request.

    Hum… not seeing the highlights post section on the category archive page I checked: http://your-domain/category/anthem/

    We aren’t using the content template block at all β€” just letting the default category page template handle everything but the hero section.

    The category archive pages use using block element – content template.

    Let me know if I miss anything!

  • I think we are both missing things β€” me more than you, though!

    You are right about the content template block. It has been a couple years since I built the site, and I completely forgot about it.

    Some of the categories – Anthem is one – don’t use the fancier hero treatment like the main categories the site is focused on. If you look under the GAMES menu and pick one of those items, you’ll get to one of the fancy category pages.

    Those fancier category pages use Page Hero Elements that include the Query Loop block. Those are the pages where the Show More button loads the additional posts immediately underneath the Query Loop block, instead of below the category’s post listing that is created with the Content Template.

    I hope that makes more sense.

  • I see what you mean, can you try adding this PHP snippet?

    add_filter( 'post_class', function( $classes ) {
    	if ( is_archive() && in_array( 'gb-query-loop-item', $classes ) ) {
    		$index = array_search('infinite-scroll-item',$classes);
    if($index !== FALSE){
    	return $classes;
    } );

    Let me know if this helps!

  • That did the trick β€” thank you!

    I have one other issue with this block. I can’t seem to vertically middle align the post headlines within the container block.

    I have searched for solutions, but when I change the settings as directed it also changes the width of the post thumbnail. I have the image set to 60px square in the block settings, and I’m using a 60px square image size I added just for these post highlight blocks.

    What am I doing wrong?

  • You can set the parent container to display:flex,align-items:center in the layout panel.

    And select the image block, and click the add to container button to wrap a container outside it.

    Then select the new container, and set flex-shrink to 0.

  • Thanks so much, Ying – it’s all looking/working great!

  • Glad to hear that πŸ™‚

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