The Post Taxonomy Terms feature in Conditional Blocks Pro lets you control the visibility of WordPress blocks based on posts and their terms.
What are Taxonomies?
WordPress taxonomies are used to group content together. You are most likely already familiar with the built-in “Categories” and “Tags” in relation to WordPress posts. Each of those is a taxonomy.
What are Taxonomy terms?
Each taxonomy will have its own terms. So what is a term? Let’s look at a Post Category for example. Each selectable category that you’ve created is known as a “Term”. You might have multiple terms that you use to categorize your posts. E.g Personal, Updates, Newsletters.
You can read more about taxonomies on the official WordPress site.
How to change the visibility of blocks based on taxonomy terms
This feature is well suited to Full Site Editing and Block Widgets because your blocks will generally appear on multiple pages. Therefore, you can find yourself in a scenario where you want to display a unique block only on posts that have a specific category (aka term).
You can selectively display blocks based on taxonomy terms using Conditional Blocks.
- Once you’ve created the block, select it and open the Condition Builder.
- Apply the Post Taxonomy Terms condition.
- Select the Post Type (Posts, Pages. or Custom Post Types)
- Select the taxonomy (Categories, Tags or Custom taxonomy)
- Search and select the terms that a post should contain.
- Configure the Block Action to make the selected block be visible or hidden when the post has the selected terms.
What to do if your Post Type is not listed
The plugin will automatically attempt to detect all available & Custom Post Types. If your CPT is not showing up, then it’s likely not supported within the WordPress REST API.
Don’t worry – The fix is easy if you hare using a plugin or code to register Post Types.
Look for the setting called “Show In REST” and enable it.
The setting will allow the post type to be visible within the WordPress Block Editor & Conditional Block.
Overriding an exiting Post Type to enable REST API
/**
* Enable REST API for a custom post type. In this example CPT is called portfolio.
*/
function cb_custom_enable_rest_for_custom_post_type($args, $post_type) {
if ('portfolio' === $post_type) {
$args['show_in_rest'] = true;
}
return $args;
}
add_filter('register_post_type_args', 'cb_custom_enable_rest_for_custom_post_type', 999, 2);
Code language: PHP (php)
Overriding an existing Taxonomy to enable REST API
In the below example the taxonomy (also known as categories) has the ID of `project-type’. You should change this to your own taxonomy id.
Tags/attributes should be automatically enabled with the snippet as well.
function cb_custom_taxonomy_rest_args( $args, $taxonomy ) {
if ( 'project-type' === $taxonomy ) {
$args['show_in_rest'] = true;
}
return $args;
}
add_filter( 'register_taxonomy_args', 'cb_custom_taxonomy_rest_args', 999, 2 );
Code language: PHP (php)