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 WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Different content blocks styles based on post-type in a query loop?

  • @Ying, I was trying to add pagination to my archive page but was looking in the wrong place. I was checking “Appearance > Customize”, but realize its a button that is part of the Query Loop block.

    All good. Thank you all for all your help! This has been a game changer.

  • Glad to hear that 🙂

  • Hey guys, sorry to come back to this so late, but curious how I would edit the code here to add a 3rd or 4th post type to the mix? I tried two different code types (below), but its not working. Any tips on what I could do to get it working?

    /* Removes REGULAR post template on LINK or VIDEO posts, 
       LINK post template on REGULAR or VIDEO posts, 
       and VIDEO post template on REGULAR or LINK posts */
    
    add_filter( 'render_block', function( $block_content, $block ) {
        if ( 
            !is_admin() && 
            ! empty( $block['attrs']['className'] ) && 
            strpos( $block['attrs']['className'], 'post-template' ) !== false 
        ) {
            $current_post_type = get_post_type();
            
            // Remove 'regular-post' template on LINK or VIDEO posts
            if ( 
                strpos( $block['attrs']['className'], 'regular-post' ) !== false && 
                $current_post_type !== 'post' 
            ) {
                return ''; // Hide block
            }
    
            // Remove 'custom-post' (LINK) template on REGULAR or VIDEO posts
            if ( 
                strpos( $block['attrs']['className'], 'custom-post' ) !== false && 
                $current_post_type !== 'links' 
            ) {
                return ''; // Hide block
            }
    
            // Remove 'video-post' template on REGULAR or LINK posts
            if ( 
                strpos( $block['attrs']['className'], 'video-post' ) !== false && 
                $current_post_type !== 'video' 
            ) {
                return ''; // Hide block
            }
        }
    
        return $block_content;
    }, 99, 2 );

    then updating the original code provided:

    /* Removes REGULAR post template on LINK post, and LINK post template on REGULAR Post */
    
    add_filter( 'render_block', function( $block_content, $block ) {
        if ( 
            !is_admin() && 
            ! empty( $block['attrs']['className'] ) && 
            strpos( $block['attrs']['className'], 'post-template' ) !== false 
        ) {
            $current_post_type = get_post_type();
            if (
                ( strpos( $block['attrs']['className'], 'regular-post' ) !== false && $current_post_type !== 'post' ) || 
                ( strpos( $block['attrs']['className'], 'custom-post' ) !== false && $current_post_type === 'post' )
            ) {
                return ''; // Return nothing if either condition is met.
            }
        }
    
        return $block_content;
    }, 99, 2 );
    
  • The first filter name is wrong, it should’ve been generateblocks_query_loop_args.

  • Thanks @Ying. That helped a bit.
    I’ll keep this as resolved and opened a new ticket as I had a few more questions about this and updating to new query block 2.0.

  • I’ve replied to your new ticket 🙂

Viewing 6 posts - 17 through 22 (of 22 total)
  • You must be logged in to reply to this topic.