Developer Docs
  • Getting started
  • Tutorials
    • Integrate your theme with HivePress
    • Create a custom HivePress extension
  • Framework
    • Blocks
      • Callback
      • Container
      • Content
      • Form
      • Menu
      • Modal
      • Part
      • Section
      • Template
      • Toggle
    • Components
      • Cache
      • Helper
      • Request
      • Router
      • Translator
    • Configurations
      • Comment types
      • Image sizes
      • Meta boxes
      • Post types
      • Scripts
      • Settings
      • Strings
      • Styles
      • Taxonomies
    • Controllers
    • Emails
    • Fields
      • Checkbox
      • Checkboxes
      • Date
      • Date Range
      • Email
      • File
      • Number
      • Number Range
      • Password
      • Phone
      • Radio Buttons
      • Repeater
      • Select
      • Text
      • Textarea
      • Time
      • URL
    • Forms
    • Menus
    • Models
      • Making queries
      • Creating models
      • Customizing models
    • Templates
  • Resources
    • Code snippets
    • Code reference
    • Hook reference
    • REST API
Powered by GitBook
On this page
  • Creating templates
  • Customizing templates

Was this helpful?

  1. Framework

Templates

PreviousCustomizing models

Last updated 2 years ago

Was this helpful?

In HivePress, templates are implemented as PHP classes that define arrays of . 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.

Creating templates

If you are developing a custom HivePress , 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.

Customizing templates

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.

You can customize any of the existing templates using . For example, the code below changes the number of columns on the Listings page to 3.

blocks
extension
hooks