Skip Navigation

[Resolved] How to get output of View to JS

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

This topic contains 2 replies, has 2 voices.

Last updated by toolset-dave 7 years ago.

Assisted by: Nigel.

Author
Posts
#590133

Hi, I have a View which shows the highest number from all CPT (https://toolset.com/forums/topic/how-to-get-highest-amount-from-all-cpt/) and I want to use it in JS. How to make the View [wpv-view name="highest-number"] instead of typed-in-value work in this JS code:

$("#price-range").slider({

  // maximum value
  max: 5000,
...
#590166

Nigel
Supporter

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

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

Hi Dave

There is a WordPress function that was originally intended for passing translations but now is commonly used for passing any required content from PHP on the server to JS on the front end, namely wp_localize_script (https://codex.wordpress.org/Function_Reference/wp_localize_script).

However, it might be simpler in this case, depending on how you are adding the JavaScript.

Say you have a Content Template for some post type, and you want to add the JavaScript to this template.

Instead of using the custom JS editor section, you could add your code directly in the content editor in script tags that will appear in the markup of your page. Which means you can use shortcodes in your JS code, something like:

$("#price-range").slider({
 
  // maximum value
  max: [wpv-view name="highest-number"],
...

I think that is your simplest solution.

#590233

Thank you, I used your simple solution and it works nice. 🙂