Archived topic
generate_do_template_part() condition? Like has_shortcode() or has_block() or ..
3 replies · Started by Elvis on May 1, 2022
Hello,
I love using the function from title.
I want to load assets only if we are using the template generated with the instance of this function. As I say in the title, we can do this, with blocks, shortcodes, is_page_template() templates ... is there a way to do it with templates generated with generate_do_template_part().
Thanks.
best,
Elvis
Hi there,
what are you trying to determine ? Whether there is a custom content template such as those from the Block Element loading ?
Hello David.
I have made a function that I insert into a page programatically using generate_do_template_part( 'apuri_staff_template' ); Now since this template requires me to load some JS in order for it to operate as I want, I would like to enqueue the JS when, or on the page where that function is ran.
If I used the shortcode to perform the same thing, I wouls do it like this:
function apuri_staff_filtering_assets() {
global $post;
if ( has_shortcode( $post->post_content, 'apuri_staff_template' ) ) {
wp_enqueue_style( 'the js file stuff' );
}
}
add_action( 'wp_enqueue_scripts', 'apuri_staff_filtering_assets', 99999, );
Is there something along the lines of:
if ( generate_do_template_part( 'apuri_staff_template' ) ) {
wp_enqueue_style( 'the js file stuff' );
}
Thanks,
e
You would probably need to check out the:
global $generate_elements;
It will store the element ID of any element on the page.