Home › Forums › Pro Support › PHP Hooks on GenerateBlocks Query Loop Block
- This topic has 6 replies, 2 voices, and was last updated 1 year, 4 months ago by
Fernando.
-
AuthorPosts
-
July 10, 2022 at 5:39 am #37064
Xavier
ParticipantHi, 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.
July 10, 2022 at 11:21 pm #37067Fernando
ParticipantHi 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!
July 10, 2022 at 11:40 pm #37070Fernando
ParticipantDidn’t notice but Pagination is already available in Query Loop Block as well: https://community.generateblocks.com/t/can-query-loop-handle-pagination/763#:~:text=With%20the%20Query%20Loop%20block%20selected
July 10, 2022 at 11:45 pm #37073Xavier
ParticipantHi 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:
My question is if this kind of hooks are still avaiable via block.
Many thanks.
July 11, 2022 at 12:27 am #37075Fernando
ParticipantYes, 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!
July 11, 2022 at 12:55 am #37078Xavier
ParticipantFantastic Fernando! Many thanks!
July 11, 2022 at 1:02 am #37080Fernando
ParticipantYou’re welcome Xavier!
-
AuthorPosts
- You must be logged in to reply to this topic.