Skip Navigation

[Resolved] Display related posts and headline only if there are any

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to show a headline and related posts only if there are related posts.

Solution: Move the headline into the View of related posts, and use a conditional that tests the number of results found in the View.

Relevant Documentation:
https://toolset.com/course-lesson/displaying-related-posts/
https://toolset.com/course-lesson/using-toolset-conditional-block/

100% of people find this useful.

This support ticket is created 3 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 2 voices.

Last updated by haraldO 3 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#1912037
2021-01-20 13_58_23-Edit Content Template ‹ Erkki Eronen Art — WordPress.jpg

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?
hidden link => has a related publication in the bottom
hidden link => has the related artwork(s) at the bottom of the page

#1912577
no-items-found.jpg

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.

#1912889
2021-01-21 08_41_30-Edit Content Template ‹ Erkki Eronen Art — WordPress.jpg

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 ..."?

#1913451
Screen Shot 2021-01-21 at 10.29.32 AM.png
Screen Shot 2021-01-21 at 10.16.29 AM.png
Screen Shot 2021-01-21 at 10.15.32 AM.png

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.

#1914695

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 🙂

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.