DescriptionDescription
This filter is called when getting templates in the admin interface (shortcodes/widgets/blocks) and to render templates on the frontend.
It allows to register your own query and markup without the grid and card system. You will thus be able to use facets with your own content.
ArgumentsArguments
Argument | Type | Description |
class | string | Class name of the layout wrapper holding posts |
source_type | string | Object type to query (post_type, term, user) |
is_main_query | boolean | To replace loop in archive/index/search.php templates |
query_args | array|callable | Holds query arguments |
render_callback | callable | The callback to be run to render content |
noresults_callback | callable | The callback to be run to render no results message |
ExampleExample
You can register templates in your child theme or plugin as follows:
function prefix_register_template( $templates ) {
// 'my_template' corresponds to the template ID.
$templates['my_template'] = [
'class' => '',
'source_type' => 'post_type',
'is_main_query' => false,
'query_args' => [
'post_type' => 'post',
'posts_per_page' => 10,
],
'render_callback' => 'prefix_render_callback',
'noresults_callback' => 'prefix_noresults_callback',
];
return $templates;
}
add_filter( 'wp_grid_builder/templates', 'prefix_register_template', 10, 1 );
prefix_render_callback
and prefix_noresults_callback
callbacks examples:
<?php
/**
* This callback is called for each post in the loop.
*
* @param object $post Holds post, term or user object (depending of the source_type).
*/
function prefix_render_callback( $post ) {
?>
<article>
<?php the_title( '<h3>', '</h3>' ); ?>
<?php the_excerpt(); ?>
</article>
<?php
}
/**
* This callback is called when no results match selected facets.
*/
function prefix_noresults_callback() {
?>
<p><?php esc_html_e( 'Sorry, no results match your search criteria.', 'text-domain' ); ?></p>
<?php
}
A template can also replace the main loop of the archive.php
, index.php
or search.php
files. In this case you need to set is_main_query
to true
.
If you set is_main_query
to true
, query_args
parameter will be ignored because it’s handle by WordPress template.
To render a template in a page, you can use the function wpgb_render_template()
.
Or the shortcode [wpgb_template id="my_template"]
or a Gutenberg block or a WordPress widget.