Tell us what you are trying to do?
I want to use a Conditional Block to wrap around related posts (many-to-many relationship) so they display only when there are posts related to the current one.
Is there any documentation that you are following?
Yes, all the videos and documentation pages, as well as several forum posts. But I couldn't find how to configure the Conditional Block to display only when related posts are available.
E.g. https://toolset.com/course-lesson/displaying-related-posts/
Is there a similar example that we can see?
Not that I know of.
What is the link to your site?
lien caché => has a related publication in the bottom
lien caché => has the related artwork(s) at the bottom of the page
Hi, I will be glad to help. There's not really a simple way to test whether or not the current post has related posts, using a simple conditional. In general, the most straightforward way to determine whether or not related posts exists is to use a View. Assuming you have already implemented a View of related posts, the View will display the related posts if any exist. If no related posts exist, the View will show "No items found" by default. You can remove that "No items found" message, and the View will show nothing instead. So the conditional block seems redundant to me, unless I've misunderstood what you want to accomplish.
To remove the "No items found" message, you must select the View Loop block in the block editor, then you can modify the "No items found text" in the Loop Template configurations panel. See the screenshot here for a visual guide.
Let me know if I've misunderstood, and I can provide more feedback.
Hi Christian,
thanks for the information, it's exactly as you mentioned. I have a view with related posts implemented. But I also want to hide the headline above the view. It's pretty useless to have a headline "These are the related XYZ" and then nothing beneath it 🙂
Do you know what I mean? Moving the headline into the loop is (of course) nonsense, because it would also be repeated. Maybe there is a conditional that would work, if I place the headline here (see image)? Like "Only show, if there are items in this view ..."?
Okay I see what you mean now, the problem is the headline that might appear even if no related posts exist. I think the most straightforward solution here is to move the Headline into the View, but not in the Loop area. Move it into the Search filters area instead. Turn on the "Search" option in the top-level View block to expose the View Search block, where you would normally display search filters. Drag your conditional block into that search area, and then drag the headline block into the conditional block for a nested structure. Then you would set up the conditional conditions to test whether the number of posts found (Views shortcode) is greater than a static value of 0 (the number zero). See the screenshots here showing the blocks arrangement in the Block Editor as well as the condition setup in the Conditional Block configurations. I'm also attaching a screenshot showing where to turn on "Search" in case this option is not obvious. Note that you must select the top-level View block to see the "Use View elements" options.
Thanks for your great support, I got it working by placing the headline in a conditional in the non-looping view area / search area like you explained! Have a great weekend 🙂