Skip Navigation

[Resolved] Load an external js file when page contains a view

This thread is resolved. Here is a description of the problem and solution.

Problem:
Load an external js file when page contains a view

Solution:
You should have to build an array of page IDs where you added your view and then use that array with if condition to check if current post/page ID is available with the array value, then you can enqueue the js.

Relevant Documentation:

This support ticket is created 3 years, 8 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 4 replies, has 2 voices.

Last updated by Paul Marconi 3 years, 8 months ago.

Assisted by: Minesh.

Author
Posts
#2079833

Hi,

With the 'Toolset Views' plugin, we have created couple of views to list CPT.
Currently, we have an external .js file enqueue in the functions.php file, so it's loading in all pages.
However, we want to have that js file loaded when a page contains a toolset view.
Is there a way to check if the current page contains a view? Therefore, we can include an if statement in the functions.php file to load that specific file if it's true.

Thank you!

#2080013

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

There is no way to check if page has view added or not.

I think the way is to build an array of page IDs where you added your view and then use that array with if condition to check if current post/page ID is available with the array value, then you can enqueue the js.

#2080583

Thanks Minesh!
Just wondering, is there a way to check if the current page contains a certain class or ID name?
Say if the current page contains the classname "has-view", in the functions.php file we can add an if statement to enqueue other js or css files.

Is there s way to do that in wordpress?

Thank you!

#2081113

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

We can check that using the jQuery but not using PHP.

The workaround I shared is better to go with page IDs.

#2084663

Thanks Minesh!