Display blocks conditionally with the WordPress site editor

Today I was working on the template used for displaying single notes and links. Because these are not technically separate post types, but instead just a single post type distinguished by different categories, I encountered what I suspect is a rather niche problem:

As these two “types” of notes have slightly different properties, I wanted to create distinct templates for single entries of each category.

According to the Site Editor documentation, it should be doable:

Category – It allows the creation of templates that can be applied to posts by a specific category on the site or as the default template for all categories (if not already defined by the theme).

Unfortunately, I found no way to do this. As is, unfortunately, too often the case when I’m referencing the Site Editor documentation. Either way, it forced me to reframe the problem a little.

The overall template would remain more or less the same for notes from both categories. It was only a few elements that would change depending on whether it resided in the “notes” category or the “links” category. Could I somehow conditionally insert blocks based on the category?

Out of the box, the answer is no.

Which means that I was looking at manually fiddling with the theme files to do this. Something which I’m loath to do, because it means more stuff to maintain. Inevitably I would forget what I did to achieve just that, and end up spending a long time reverse engineering it at some point in the future.

So instead I set out to find a plugin that would allow me to do this. First hits were some complete overkills that had this functionality behind a premium paywall priced in the hundreds of dollars range. That’s a bit too rich for my personal blog.

But, after a bit of looking around, I found the Block Logic plugin. It’s lightweight, and lets me achieve exactly what I want to do. And it does it in an elegant way, because you can use WordPress’ native conditional tags to generate the condition for whether or not you want to display the block.

So all I have to do is input the “has_category()” tag as a condition for the relevant blocks, like so:

has_category( 'notes' )

or

has_category( 'links' )

And voila, the block will only be displayed when the post belongs to the specified category. Using a container block with the conditional, that means I can for instance easily show the relevant listing below the actual note, despite notes and links using the same template:

Image showing a single link post and a single note post side by side
Single link post on the left and single note post on the right are using the same template. But with the Block Logic plugin I can use the category of the note to determine which blocks to display.

I can immediately think of a variety of scenarios where the ability to display blocks based on conditions would be useful. Both inside templates, as well as within actual content. In those cases, I will definitely be taking advantage of this plugin again.


Responses

Respond to this post with a webmention, a reply from your fediverse account or a comment using the form below.

Want to discuss in private?

 

Email

 

me.

Leave a Reply

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