Skip Navigation

[Closed] WPV conditional end of slug

This support ticket is created 4 years, 6 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)

Tagged: 

This topic contains 10 replies, has 2 voices.

Last updated by Christian Cox 4 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#1649407

Running WPML + Toolset, I'm going to create pretty complex forms and views with many custom fields, taxonomies, etc. in several languages.

I don't want to update all the views and forms when I add a language and then forget about something.

I'll take as example category taxonomy, but the solution might work for everything else.

Example for category slugs:
- in English: car-11, bike-22, plane-33, boat-44
- in French: voiture-11, velo-22, avion-33, bateau-44,
- in German: auto-11, fahrrad-22, flugzeug-33, boot-44.

Then I use this wpv-conditional to display content based on following conditions: must be cars and bikes, doesn't matter the language.

[wpv-conditional if="( '[wpv-post-taxonomy type='category' format='slug']' eq ARRAY('car-11', 'bike-22', 'voiture-11', 'velo-22', 'auto-11', 'fahrrad-22') )" evaluate="true"]They have wheels![/wpv-conditional]

That solution actually works: I manually added all the relevant slugs. But if tomorrow I add a language, I've to update each view and maybe forget one line or make a mistake.

Question is: how can I display the slugs depending on the end of it?

You can notice that translations have the same numbers at the end. With such solution, I could just write '11' and '22' and it will display both cars and bikes, doesn't matter on what language. And I'll just have to add the right numbers when I add a new translation for a taxonomy or whatever, without updating Views and Forms.

That doesn't work of course:

[wpv-conditional if="( '[wpv-post-taxonomy type='category' format='slug']' eq ARRAY('11', '22',) )" evaluate="true"]They have wheels![/wpv-conditional]

EDIT:
I guess I've to use something like this: hidden link
But I don't know how to implement that inside wpv-conditional.

#1649415

Hi, you may be able to use the RIGHT function:
https://toolset.com/documentation/user-guides/front-end-forms/cred-conditional-display-engine/#functions
This function should allow you to inspect a substring from the end of the term and continuing a certain number of characters:

[wpv-conditional if="( RIGHT('[wpv-post-taxonomy type='category' format='slug']' , 2) eq RIGHT('11',2))" debug="true"]slug ends in 11[/wpv-conditional]
              [wpv-conditional if="( RIGHT('[wpv-post-taxonomy type='category' format='slug']' , 2) eq RIGHT('22',2))" debug="true"]slug ends in 22[/wpv-conditional]

This only works if the slugs only contain 2 digits. If you must include more than 2 digits, the function will not work correctly because -111 and -11 will evaluate identically.

#1649419

Thanks Christian,

then can I use letters rather than numbers?

Also, can I do use ARRAY to have all parameters in one single bracket?

#1649421

Forgot to mention another limitation is that only one term from the 'category' taxonomy may be applied to any post. If more than one term is applied, you'll need a custom code solution to iterate over those terms and evaluate each one.

#1649423

Yes, you can use letters, but no you cannot use ARRAY in this comparison. You must use separate conditionals for each suffix.

#1649733

I'm not sure to understand this: "Forgot to mention another limitation is that only one term from the 'category' taxonomy may be applied to any post."

Anyway.

The 2 digits is a limitation (logic though...).

When I create a new custom field, I can edit "appears on" terms I select, like specific categories. Therefore, on the backoffice, I see only the relevant custom fields.

Is there a way to use that when I display views and CRED forms?

Basically, my main concern is to have simple shortcodes I could keep as they are even if I add languages. I prefer to check boxes somewhere or write specific slugs rather than touching the shortcodes.

That's why my idea was to add "codes" (digits or letters) at the end of each category and each translation would have the same code.

Do you have other ideas to reach that goal?

#1650613

I'm not sure to understand this: "Forgot to mention another limitation is that only one term from the 'category' taxonomy may be applied to any post."
I mean that this conditional system may not work if you add more than one category term to any post. I'm not sure how your content is structured so I don't know if this is a problem or not. The wpv-post-taxonomy shortcode returns all the terms from the category taxonomy. So if you apply multiple terms like car-11, bike-22, and helicopter-99, then the wpv-post-taxonomy shortcode will return all of those terms together. If you set up a conditional to test if the term ends in 11, this could be a problem. The RIGHT function does not consider each of the terms separately, so you might get a false reading even though one of the terms ends in 11.
If you need to check each term independently to see if any of the terms end in 11, then you probably need a different solution.

The 2 digits is a limitation (logic though...).
Of course you could make it 3 characters or 4 characters or more, but it has to be consistent across all terms. That means the slugs you have already set up with two-character suffixes may need to be changed to accommodate the 3 character conditional test.

Is there a way to use that when I display views and CRED forms? Do you have other ideas to reach that goal?
Nothing built-in to the system, but perhaps you could use custom term fields in a taxonomy View's Query Filter instead of using wpv-conditional statements to test the term slugs. In other words, use a View's query filter as a conditional. For example, create a custom term field checkbox for "show on View 1234". Check that checkbox on all translations of the same term, and then use that term field as a Query Filter in a View of terms. If any results are found, you know that the post containing that term should be shown on in View 1234, or on Page XYZ, or any other specific placement. Set the translation status for that field to be copied from the primary language into all secondary languages, and as your site's number of secondary languages grows there are no additional changes to make. Instead of touching the View filter or other shortcodes, you manage the display in each term's corresponding checkbox field.

This approach only works if testing terms that have been applied to existing posts. It cannot be used to show and hide sections of a Form that creates a new post or edits an existing post, by responding to terms as they are selected in the Form. To respond to Form inputs in real-time, you must use cred_conditional_group shortcodes and the cred conditional display functions: https://toolset.com/documentation/user-guides/front-end-forms/cred-conditional-display-engine/

#1652757

I guess that could also work to deal with WPML:

[wpmp-string][wpv-conditional if="( '[wpv-post-taxonomy type='category' format='slug']' eq ARRAY('car-11', 'bike-22'') )" evaluate="true"]They have wheels![/wpv-conditional][/wpml-string]

It cannot be used to show and hide sections of a Form that creates a new post or edits an existing post, by responding to terms as they are selected in the Form.
Then that could work on the form to edit a post, considering the categories saved earlier? The form I was thinking about was just to add new elements based on categories of the post (as condition), without the possibility to edit the category selection anyway.

In fact the main issue is also to deal with WPML.

#1654451

Then that could work on the form to edit a post, considering the categories saved earlier?
If I recall correctly we were discussing the possibility of using a taxonomy View with a taxonomy term Query Filter as a conditional to display or hide some arbitrary site content. In theory yes, you could place some of the Form's input fields in the output of that taxonomy View, and use a term Query Filter to determine whether or not those fields should be displayed. Place this View inside the Form where the fields would normally be displayed, and you could use shortcode attributes in the View to pass in selected term IDs or slugs or whatever your filters need.

#1657719

Thank you Christian.

Do you have a generic code example? Or screenshot?

Correct me if I'm wrong: even with Block plugin, do I need View to achieve that?

#1662157

Basically you would use the 'expert mode' in the Form builder to see the form shortcodes. One form shortcode might look like this:

[cred_field name="my-field"]

You would remove that field shortcode and place it in the body of a View, then place that View in the expert Form builder using the View shortcode:

[wpv-view name="my-view"]

You can use the Blocks plugin here, but you'll need to use shortcodes to place Form fields in the View and you'll need to use the expert Form builder to place the View shortcode in the Form.

The topic ‘[Closed] WPV conditional end of slug’ is closed to new replies.