Skip Navigation

[Résolu] How to get dynamic var in shortcode

This support ticket is created Il y a 4 années et 6 mois. 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 8 réponses, has 2 voix.

Last updated by fred-r.M Il y a 4 années et 6 mois.

Assisted by: Beda.

Auteur
Publications
#1334091

Tell us what you are trying to do?

On a custom page hidden link we have on a tab.

In this tab (those tabs are separate post types from its plugin), I like to have showed custom fields. Those custom fields are basically coming from this own custom page. So referenced to a id. In the tab I use the short code:

[types field='accommodation-type' item='10'][/types]

This is working. But now I like to have instead of item='10' - a variable, referenced to the main custom type (post type). How can I reference this. If I use a short code getting a post ID, I get the post ID from the tab.

Somehow, I need the post ID from the custom page.

Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site?

hidden link

#1334131

If the Page or Post displayed with [types field='accommodation-type' item='10'][/types] is not the current page, then there is no way to tell a code, to grab another post dynamically, unless you somehow can point the code into the direction (which is basically a Query) to take, to get the right post.

How do you "know" what post will need to show for [types field='accommodation-type' item='10'][/types]?
That is what we need to tell the code as well. This will either be possible using a View or, custom code - this in case there is no relation the View would be able to understand.

Can you elaborate, what the connection of tab to [types field='accommodation-type' item='10'][/types] is?
Basically, how do you know which Post ID to pass? That precise same "process" is what we need to "teach" to the application, in this case.

I can help you with setting something up, after knowing how exactly the relation between these elements looks alike

#1334181
postid10.png
tabid.png

Dear Beda

I understand what You mean - I just not know how to get there. I was thinking, if I add in my single.php a global var, to get the id of the "outer" post.

with something like this:

<? global $wp_query; $postID = $wp_query->post->ID; ?>

So, on this page, it is basically a post page (hidden link), is done from custom types.

Inside this page, we have those tabs (image tabid). those text, I get, when I use [types field='accommodation-type' item='10'][/types] respectively item='10' - as this is the post id from my custom type. In there, I have custom fields.

So, first I need to find the "outer" post id. I know in there:

<body class="pool-villas-template-default single single-pool-villas postid-10 l-body Impreza_6.1.2 us-core_6.1.3 header_hor headerinpos_top state_default wpb-js-composer js-comp-ver-6.0.5 vc_responsive" itemscope itemtype="<em><u>hidden link</u></em>">

I am having the postid-10... maybe can filter this out...

Once I have this, I need as second part, the shortcode changed, so I can use a variable instead a fixed string:

something like this: [types field='accommodation-type' item="'. $postID . '"] or [types field='accommodation-type' item=\"$postID\"]

I appreciate if You might know how to do this.

Regards

#1334201

I understand.

In this case, hidden link is Post with ID 10, hence that is the data you get when calling [types field='accommodation-type' item='10']

If the ShortCode [types field='accommodation-type' item='10'] is inserted in a Content Template that is applied to the post, or in the post body of that post, no matter if this is in a tab or popup, you do not need to pass an item attribute at all, because the ShortCode will default to display the current post which is hidden link, or post id 10.

If instead you display [types field='accommodation-type' item='10'] inside a View, that queries another post type, or even the same post type, but many of them, and you need to bring out the one belonging to the "container" page where the View is inserted to, you could use the item attribute value $current_page.

This is an inbuilt variable for Toolset item attributes, allowing to get the container ID of the currently displayed View or another kind of element.
You can consult this DOC for many more details: https://toolset.com/documentation/user-guides/views-shortcodes/item-attribute/

Is it this you are after?

#1334241

Dear Beda

I understand what You mean. But as [types field='accommodation-type' item='10'] or without [types field='accommodation-type'] is inside "another" post - this post from the tap (this is a plugin and used its own custom types, I will get basically nothing showing, as long as I use [types field='accommodation-type'].

I do not use a view or layout on the "main"-post - as we use visual composer and this part from the theme.

so do I might need to use the item=${current_page} ? Or how can I use it? If You can show me the exact example:

Like this? [types field='accommodation-type' item=${current_page}][/types]

Regards

#1336319

I understand the plugin you use to generate the tabs is basically generating a loop. Like Views would.

The usage of $current_page is elaborated here:
https://toolset.com/documentation/user-guides/views-shortcodes/item-attribute/

I am not sure why you mention item=${current_page}.
This syntax is not mentioned on the doc.
The syntax is $current_page, as it shows here:
$current_page is used to retrieve data from the current page being rendered. It can be used inside a View loop to obtain data from the page on which the View is being rendered.

Hence:
item="$current_page"

That will catch the ID of the page where the Loop (of Toolset) is used.
I am not sure, this will work inside other plugins' loops, but it is worth to try.

If that does not work Toolset cannot help getting the right post as there is no way for it to control the 3rd party loop and what it does/if it has a container object, etc.

But we could eventually solve this with some Custom ShortCode, like one getting the current page and returning it's ID, as a start hidden link

Please let me know if $current_page works, if not I can help with some custom ShortCode (review and guidance, but not full coding)

Thanks!

#1336693

Dear Beda

I did like this, and it is working so far: [types field='full-video' item=$current_page][/types]

I was sure I saw it somewhere that is written somehow in a different way. But anyway it worked for now.

I have another issue, where I am not sure how to do:

Using this part is working fine in a layout. But as we not use layout for some posts it not working, especially not for youtube or vimeo video url.

<p>[wpv-conditional if="( $(wpcf-location-video) ne '' )"]<iframe width="853" height="480" src="[types field='location-video' output='raw'][/types]" frameborder="0" allowfullscreen=""></iframe>[/wpv-conditional]<br />
[wpv-conditional if="( $(wpcf-vimeo-video) ne '' )"]<iframe width="853" height="480" src="[types field='vimeo-video' output='raw'][/types]" frameborder="0" allowfullscreen=""></iframe>[/wpv-conditional]  </p>
<p>[wpv-conditional if="( $(wpcf-location-video) eq '' ) AND ( $(wpcf-vimeo-video) eq '' )"][wpv-post-featured-image size='center' class='img-responsive']</p>
<div id="pin_btm"><a data-pin-do="buttonBookmark" href="<em><u>hidden link</u></em>" data-pin-tall="true"></a></div>
<p>[/wpv-conditional]</p>

I get the url, when I am only using [types field='location-video' output='raw'][/types], but once I put the iframe around - it is not working anymore.

Any idea what I can do?

Here is a link You can see it: hidden link

I am also facing somehow issues, when using the conditional. In some places I not need to have the "ending" and closing conditional, then it is working fine.

#1337413

The syntax you saw likely relates to the one used as compatibility syntax for Toolset, that produces {!{shortcode}!} instead of [shortcode]
Or, you have seen it in some code example, maybe, where it was intended to show a "variable" to change in code?

The one you use now is correct.

I am not sure about the second issue. We should handle this in another thread if it persists, and close this one.
I understand the exact same HTML code you paste works in Layouts but if you insert it in any non-Layouts editor like WordPress post editor it doesn't?
That might be due to the Gutenberg Editor, it strips certain apostrophes and replaces them, it can break HTML.

If that is not the issue, can you open a new ticket describing the new problem so we can handle it adequately?

Thanks

#1337685

My issue is resolved now. Thank you!

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