Show the entries of a CTP category in Osom Blocks

show-entries-CPT-category-Osom-Blocks

Discover how you can display the entries of a CTP category using the Osom Blocks plugin, with a little bit of code.

Displaying entries from Custom Post Types using a block is not easy, since native blocks only support entries and pages. That’s why at OsomPress we created the plugin Osom Blocks.

This one allows you to create a listing of any post type (CPTs, posts and pages) and customize it using the user interface. Among the settings are:

  1. Custom content type.
  2. Number of entries.
  3. Show/hide the featured image.
  4. Show/hide excerpt.
  5. Choose the grid layout.
  6. Choose the number of columns for the grid layout.
  7. Choose the HTML markup for the title (H2, H3 or H4).
  8. Customize the “Read more” text.
  9. Show/hide parent entries/pages (only in hierarchical custom content types).
  10. Show/hide pagination.
screenshot-osom-blocks

But you may have to modify the query beyond these options on occasion.

The idea is to keep the plugin simple to use, so we don’t want to clutter the panel with too many options.

So we have added (in version 1.2) a PHP filter that allows you to access the block query and modify it easily.

This allows you to combine it with conditional tags to achieve, for example, that on a particular page only the entries of a category of a Custom Post Type are displayed.

Let’s see how:

Snippet to show the entries of a category of a CTP in Osom Blocks

Add an Osom Blocks block and configure the settings to your liking. The “Select content type” section you can leave it as it is, as this is the part we are going to modify using the filter.

Now, add the following code at the end of functions.php or in your functionality plugin:

// Show the entries of a category of a CTP in Osom Blocks
add_filter( 'osom_blocks_args', 'cg_filter_category_post_types' );
function cg_display_several_post_types( $args ) {
	if ( is_page('44') ){
		$args['post_type'] = array('portfolio');
		$args['portfolio_category'] = 'development';
	}
	return $args;
}

In this code you use the osom_blocks_args filter to modify the query and ask it to display the 'portfolio' entries on the page with ID=44 ( is_page(44) ) of the website.

You can modify both the type of entry (CPT or ‘page’) and the category of the entry ('portfolio_category') by the ones you are interested in and use another conditional tag (for example, is_home, is_front_page, is_archive…).

You can see that having a filter that gives you access to the query gives you extra flexibility. 

Keep in mind that since you are using a PHP filter, you have the limitation that you will only see the result in the frontend of the web, in the backend the changes will not be reflected.

Conclusions

You can see how easy it is to modify the query of an Osom Blocks block to show the entries of a specific category of a Custom Post Type.

Filed under

,

Nahuai Badiola Avatar

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *