We're merging with GenerateBlocks! Learn more here.

[Support request] PHP Hooks on GenerateBlocks Query Loop Block

Please login to receive premium support.

Support for the free plugin can be found here.

Home Forums Pro Support PHP Hooks on GenerateBlocks Query Loop Block

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #37064
    Xavier
    Participant

    Hi, one of the main features I loved of wp show posts was the possibility of create custom layouts that push custom meta data of each posts easily with some php hooks and code snippets.

    Is this still possible with the new block added on Generateblocks?

    Furthermore, is there any difference in features between both tools (The query block and the former plugin)?

    Many thanks.

    #37067
    Fernando
    Participant

    Hi Xavier,

    In the new GB Query Loop Block, you wouldn’t need custom PHP to alter its look. The GB Query Loop Block can easily be altered through the Block Editor.

    You should still be able to alter its HTML through PHP though. Specifically, filter through render_block.

    The Query Loop Block has more Query Parameter settings: https://docs.generateblocks.com/article/query-loop-overview/

    However, it still doesn’t have pagination, and the carousel feature which are available in WPSP Pro.

    These are planned features though for GB Pro, and there are more features planned for the GB Query Loop Block.

    Hope this clarifies!

    #37070
    Fernando
    Participant
    #37073
    Xavier
    Participant

    Hi Fernando, it is not matter of styling the block or just pagination. It is the possibility to add, for example, custom information pulled off custom fields.

    For example, in a website I have used this snippet:

    add_action( 'wpsp_after_content', function( $settings ) {
        if ( 12 === $settings['list_id'] ) {
       $centro = get_field( 'centro_investigacion' );
       
       if ( $centro ) {
    echo '<b>Centre de recerca:</b> ';
       foreach ( $centro as $post_ids ) {
       $link = get_permalink( $post_ids );
       $meta = get_the_title( $post_ids );
       echo "<br></br><a href='$link' target='_blank'>$meta</a> ";
       
    }
    }         
    }
    } );
    
    add_action( 'wpsp_after_content', function( $settings ) {
        if ( 12 === $settings['list_id'] ) {
       $area = get_field( 'area_investigacion' );
       
       if ( $area ) {
    echo '<br></br><br></br><b>Àrea de recerca:</b> ';
       foreach ( $area as $post_ids ) {
       $link = get_permalink( $post_ids );
       $meta = get_the_title( $post_ids );
       echo "<br></br><a href='$link' target='_blank'>$meta</a> ";
       
    }
    }         
    }
    } );
    
    add_action( 'wpsp_after_content', function( $settings ) {
        if ( 12 === $settings['list_id'] ) {
       $proyectos = get_field( 'proyectos' );
       
       if ( $proyectos ) {
    echo '<br></br><br></br><b>Projectes:</b> ';
       foreach ( $proyectos as $post_ids ) {
       $link = get_permalink( $post_ids );
       $meta = get_the_title( $post_ids );
       echo "<br></br><a href='$link' target='_blank'>$meta</a> ";
       
    }
    }         
    }
    } );
    
    add_action( 'wpsp_after_content', function( $settings ) {
        if ( 12 === $settings['list_id'] ) {
            $meta = get_post_meta( get_the_ID(), 'enlace_a_orcid', true );
    		$meta2 = get_post_meta( get_the_ID(), 'enlace_researchgate', true );
    		$meta3 = get_post_meta( get_the_ID(), 'enlace_academia', true );
    		$meta4 = get_post_meta( get_the_ID(), 'enlace_grec', true );
           echo "<br></br><br></br>";
            if ( $meta ) {
                echo "<a href='$meta' target='_blank'><img src='https://ilercovid.com/wp-content/uploads/2021/07/orcid-logo.png'></a> ";}
    		if ( $meta2 ) {
    			echo "<a href='$meta2' target='_blank'><img src='https://ilercovid.com/wp-content/uploads/2021/07/logo-researchgate.png'></a> ";
            }
    		if ( $meta3 ) {
    			echo "<a href='$meta3' target='_blank'><img src='https://ilercovid.com/wp-content/uploads/2021/07/academia.png'></a> ";
            }
    		if ( $meta4 ) {
    			echo "<a href='$meta4' target='_blank'><img src='https://ilercovid.com/wp-content/uploads/2021/07/grec-logo.png'></a> ";
            }
    		
        }
    } );
    
    

    To achieve the result you see in this page:

    https://ilercovid.com/equip/

    My question is if this kind of hooks are still avaiable via block.

    Many thanks.

    #37075
    Fernando
    Participant

    Yes, you can definitely do that in GB Query Loops.

    GenerateBlocks Headline Blocks have the capability to retrieve custom Post Meta texts.

    GenerateBlocks Image Blocks can also retrieve post meta images.

    You can integrate both of these Blocks within a GB Query Loop.

    In terms of code, you can also do this simply through the render_block filter as mentioned.

    So for instance, here’s a very simple sample for the first instance of centro_investigacion:

    function insert_centro_investigacion( $block_content, $block ) {
        if ( ! is_admin() && ! empty( $block['attrs']['className'] ) && 'my-custom-headline' === $block['attrs']['className']  ) {
    
    		$centro = get_field( 'centro_investigacion' );
    		
    		$my_replace = $centro[0];
                    $link = get_permalink( $my_replace );
                    $meta = get_the_title( $my_replace );
    		$block_content = '<br></br><a href=' . $link . ' target="_blank">' . $meta . '</a>';
    		
    		return  $block_content . ;
        } 
        return $block_content;
    }
     
    add_filter( 'render_block', 'insert_centro_investigacion', 10, 2 );

    Basically, if you have a Headline Block with class: my-custom-headline, you can replace the content, and add your custom text field as such.

    Another similar filter would be generateblocks_dynamic_content_output for dynamic GB Blocks.

    Hope this clarifies!

    #37078
    Xavier
    Participant

    Fantastic Fernando! Many thanks!

    #37080
    Fernando
    Participant

    You’re welcome Xavier!

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