Skip Navigation

[Resolved] Enqueuing script based on content template does not work if dynamically assigned

This support ticket is created 2 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 2 replies, has 2 voices.

Last updated by Chris 2 years, 8 months ago.

Assisted by: Shane.

Author
Posts
#2337113

I want to detect if a content template is assigned to a post and conditionally enqueue a script.

Works if content template is manually selected for each newly created post.

Does not seem to work if content template is dynamically assigned to a custom post type via the 'Usage' settings.

$post_id = get_the_ID();
$ct_id = get_post_meta( $post_id, "_views_template", true );
if ( $ct_id == 22244 || $ct_id == 30330 ) {
wp_enqueue_script( 'section-nav-script', get_stylesheet_directory_uri() . '/assets/js/dist/section-nav.js', array(), CHILD_THEME_VERSION );
}

#2337169

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Chris,

Thank you for getting in touch.

You should be able to make use of the function below to check the content template that is assigned to a particular post.
https://toolset.com/documentation/programmer-reference/views-api/#has_wpv_content_template

This provides an exact solution for your use case.

Thanks,
Shane

#2338703

My issue is resolved now. Thank you!