Skip Navigation

[Resolved] Output Custom Fields conditionally from current post, inside a View

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

Problem: I have some conditional custom fields. If custom field A has option 1 selected, I want to display custom field B's value on the current post. If custom field A has option 2 selected, I want to display custom field C on the current post.

Solution: There's no need for a View since you're placing these shortcodes on the single post page. The Types and Views shortcodes will display information about the current post, by design. Move your conditionals into the Content Template.

This support ticket is created 6 years, 1 month 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.

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 26 replies, has 3 voices.

Last updated by Ben 6 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#1124782

Ben

I apologise for this!

I believe I have now found out what was causing the problem. Could you try to login again now with the original credentials?

#1124825

I was able to log in this time! I'm investigating and I will update you soon.

#1124836

Ben

Thank you Christian, I appreciate your help!

I've noticed you're past your working hours. If its not a quick fix, I am more that happy to continue tomorrow when you get back online.

#1124855

Okay I see what's happening. The View that you're using to show the URLs is set up to loop over all Test CPTs, with no query filter: yoursite.com/wp-admin/admin.php?page=views-editor&view_id=1621

This means that on every CPT page, the View will loop over ALL the CPTs and output whichever URL is associated with the current post in the Loop (not the current post at this webpage). That's not necessary, you only want to know the custom field value for the current post. So no View is required, and Types will assume the correct context if you put the conditional code directly in the Elementor template.

I copied the conditional code from the View and pasted it in a text module in your Elementor template. Then I deleted the View cell...sorry my Elementor layout skills are not very good but you can see how it's set up. The View is not necessary, unless you want to create a filtered list of posts and show the URL for each one.

#1124859

Ben

Ah right, I see! Thank you very much for all the information and the demo setup. I think I understand it a lot better now so hopefully I won't make a similar mistake in the future. You've now set up the Elementor template to show exactly what I was expecting on each of the CPTs. Thank you for that.

I have one more question leading from this if it's ok to ask.

Imagine I had a large amount of data. For example 5000 different "Types" that could be selected, therefore 5000 different URLs that could be displayed. How could I go about doing this without having to manually generate the code from Views each time and updating the template?

Is there any solution that I can use so as I add different "Types", the URLs are always displayed without having to update any templates?

If it would be easier to have access to explain, please send the private form and I will reinstate the login credentials tomorrow if you.

New threads created by Christian Cox and linked to this one are listed below:

https://toolset.com/forums/topic/split-output-custom-fields-conditionally-from-current-post-only/

#1125340

I've split that question into another ticket, let's follow up there.

#1125377

Ben

Thank you Christian. We will follow up there.

Is there any chance you could delete the images Test Type 1.png and Test Type 2.png that I posted on reply 1123819 as they display a URL.

Thank you so much for all your help with this topic. It's really appreciated.

#1125528

Of course, I have deleted those images from the reply.

#1125529

Ben

Thank you for doing that Christian.

And thank you for all your help with resolving this issue.

#1125551

Ben

Sorry to be an even bigger pain. Could I please request that the URL in reply 1124855 is deleted?

Thank you in advance.

#1125634

No problem, I have deleted it. FYI, all full URLs you include in a forum ticket are obscured (you can test this by visiting the post in an incognito browser or by logging out of Toolset). Only the logged-in author of the ticket and our support staff can see URLs you share.

#1126274

Ben

Brilliant! Thank you again for all your help with this.

Thank you for the information as well.