Skip Navigation

[Résolu] Enqueuing script based on content template does not work if dynamically assigned

This support ticket is created Il y a 2 années et 8 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.

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)

Ce sujet contient 2 réponses, a 2 voix.

Dernière mise à jour par Chris Il y a 2 années et 8 mois.

Assisté par: Shane.

Auteur
Publications
#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

Les langues: Anglais (English )

Fuseau horaire: 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!