Skip Navigation

[Resolved] Use "item" with the slug name instead of ID

This support ticket is created 5 years, 2 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.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Tagged: 

This topic contains 9 replies, has 2 voices.

Last updated by philippeS-4 5 years, 1 month ago.

Assisted by: Nigel.

Author
Posts
#1390847
Screenshot 2019-11-25 at 07.58.47.png

Hi there,

I am building a website for our conferences. I have a fair amount of variables which changes every year, like prices, booking links and so on. What I have done is created a CPT with fields to store the values. So created a single post with all those values and using them across the website.

Now to get the value of these "variables" I use the item attribute to reference the specific post, see example below

<p>
[wpv-conditional if="( $(wpcf-early-bird-date).item(34783) lt 'TODAY()' )"]</p>
<p style="color: #dcdcdc; font-weight: bold;">[types field='price-currency' item='34783'][/types] [types field='standard-delegate-eb-price' item='34783'][/types]<br />[/wpv-conditional][wpv-conditional if="( $(wpcf-early-bird-date).item(34783) gte 'TODAY()' )"]</p>
<p style="font-weight: bold;">[types field='price-currency' item='34783'][/types] [types field='standard-delegate-eb-price' item='34783'][/types]<br />[/wpv-conditional]</p>

Example of the site: hidden link

This is working fine, but I have 2 questions:

1) Is there is a better way to do this?
2) As we have about 10 conference websites which will be using the same model. Is it possible to have the item attribute use a slug or something generic instead of the post ID, or a variable where I would set the ID. This would allow me to use the same code without changing the ID for each site?

Thanks for your help,

PS: I am using Divi

#1390939

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Philippe

The item attribute doesn't support passing slugs (https://toolset.com/documentation/user-guides/views-shortcodes/item-attribute/).

I can see how you are using a post to store global variables (there isn't really an alternative with Toolset). To be able to refer to them using a more user-friendly slug rather than the post ID, you would need to register a custom shortcode where you pass the slug as an attribute and it returns the ID of the corresponding post.

You can use the following:

/**
 * Register 'item' shortcode, returns post ID of post
 * specified by attribute 'slug'
 */
add_shortcode( 'item', function( $atts = [] ){

	if ( isset( $atts['slug'] ) ){
		$targets = get_posts( array( 'post_name__in' => array( $atts['slug'] ) ) );

		if ( is_array( $targets ) ){
			return $targets[0]->ID;
		}
	}
});

So the shortcode [item slug='hello-world'] would return 1 (the ID of the "Hello World!" post).

You can use it where you use the item attribute in shortcodes, and also in the conditional statements, i.e.

[wpv-conditional if="( $(wpcf-early-bird-date).item([item='my-target-post']) lt 'TODAY()' )"]
[types field='price-currency' item='[item='my-target-post']'][/types]
#1391159

Hi Neal,

Thanks for your reply and code provided, just not sure where to add the custom shortcode, first time I am dealing with this. Could you send me some instruction.

Thanks,

#1391181

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

You can add PHP code snippets at Toolset > Settings > Custom Code, create a new snippet and add this code to it, then activate it.

(Or you can add the code to your theme's functions.php file, but the benefit of adding it in the Toolset settings is that it will still work if you change theme.)

#1391261
Screenshot 2019-11-25 at 15.50.31.png
Screenshot 2019-11-25 at 15.51.04.png

Thanks for that, but not getting much "love" out of it... Snippet pass the test without any issue.

Using ID<br>
[types field='platinum-price' item='521'][/types]<br>
Using slug to ID<br>
[types field='platinum-price' item='[item='conf-settings']'][/types]

Getting the following result:

Using ID
12345
Using slug to ID

url: hidden link
to access the page you will need
user: tstfdevl
pwd: boris05

If needed I can give you access to the site.

Thanks for your continuous support.

#1391341

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Sorry, I think it is just a question of being careful with single or double quotes.

The conditional shortcode version looks okay, with the types shortcode you need to switch quotes, so try

[types field='platinum-price' item='[item="conf-settings"]'][/types]

And, of course, double-check that the slug of the correct page is "conf-settings".

Can you check that?

#1391537
Screenshot 2019-11-25 at 18.26.29.png

Very sorry Nigel, but I can't get it to work - slug is OK as per pictures and also added the double quotes.

Thanks

#1392131

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Could I get credentials for your site to take a look and see if I can spot the problem?

It worked fine on my local test site.

#1392371

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Philippe

Okay, it is working now.

For some reason when copying the code where I had post_name__in the double underscore had become a single underscore in your code (i.e. post_name_in).

And... I neglected to tell you that you needed to add the custom shortcode item to the settings for third party shortcode arguments at Toolset > Settings > Front-end content.

And... I wrote the code and tested it locally to accept a 'slug' argument, but the examples I gave you didn't include an argument correctly, sorry about that.

This is the correct format for the shortcode:

[item slug="conf-settings"]

I confirmed that now works correctly on your test page, and I'm confident if you want to use it in conditional shortcodes that will work, too, in the format

[wpv-conditional if="( $(wpcf-early-bird-date).item([item slug='conf-settings']) lt 'TODAY()' )"]
#1392503

My issue is resolved now. Thank you!