bitempl

How to add extra metadata to a post item

Est. reading time: < 1 minute

In order to add post meta to the post item template You need to use the filter hook 'posts-grid-builder/post-data'. One argument is passed to the callback function, this is an array with the post data, add the needed data to it.

For example, you need to display the post meta “subtitle”:

add_filter( 'posts-grid-builder/post-data', 'add_data_to_grid_post' );

function add_data_to_grid_post( $post ) {
	$post['subtitle'] = get_post_meta( $post['id'], 'key_subtitle', true );
	return $post;
}

Now copy the item template You need into your theme, as described in section “How to change a post item html layout”. In the copied to the theme item template insert the line into the necessary place of layout

<h5 class="pgb_item-subtitle" v-if="itemData.subtitle">{{itemData.subtitle}}</h5>

Where:

  • itemData is an object with post data
  • v-if is a directive for conditional block rendering, the block will only be rendered if the directive’s expression returns a truthy value
  • {{...}}  is a text interpolation using the “Mustache” syntax (double curly braces), the mustache tag will be replaced with the value of the itemData.subtitle