Templates
In HivePress, templates are implemented as PHP classes that define arrays of blocks. With blocks, it’s easy to re-use and customize specific layout parts without affecting the whole template. You can always check the available templates in the
includes/templates
subdirectory of HivePress or its extensions.If you are developing a custom HivePress extension, you may need to create a new template. To do this, create a new
class-{template-name}.php
file (use lowercase letters, numbers, and hyphens only) in the includes/templates
extension subdirectory and HivePress will load it automatically.Then, define the template PHP class. The class name should be based on the file name but with underscores instead of hyphens and no lowercase restriction (e.g.
Foo_Bar
class for the class-foo-bar.php
file). Pick a name that is unique enough to avoid conflicts with other HivePress templates.<?php
namespace HivePress\Templates;
use HivePress\Helpers as hp;
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
class Foo_Bar extends Page_Wide {
public function __construct( $args = [] ) {
$args = hp\merge_trees(
[
'blocks' => [
'page_content' => [
'blocks' => [
'listing_search_form' => [
'type' => 'listing_search_form',
'_order' => 123,
],
'listings' => [
'type' => 'listings',
'number' => 9,
'columns' => 3,
'_order' => 321,
],
],
],
],
],
$args
);
parent::__construct( $args );
}
}
The code example above implements a template based on the
Page_Wide
template and adds the Listing Search Form along with the Listings block that displays 9 recent listings in 3 columns. These blocks are added to the page_content
block inherited from the Page_Wide
template.In the same way, you can create custom templates for your HivePress extension.
You can customize any of the existing templates using hooks. For example, the code below changes the number of columns on the Listings page to 3.
add_filter(
'hivepress/v1/templates/listings_view_page',
function( $template ) {
return hivepress()->helper->merge_trees(
$template,
[
'blocks' => [
'listings' => [
'columns' => 3,
],
],
]
);
}
);
Similarly, you can customize any template in HivePress or its extensions.
Last modified 9mo ago