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