admin管理员组

文章数量:1323335

I want to know custom page template name (the one chosen in page attributes - template) in PHP, before page opens in order to enqueue additional scripts according to chosen template. I know how to get this information in javascript admin frontend part, however it would be more convenient if I could get it in PHP.

I want to know custom page template name (the one chosen in page attributes - template) in PHP, before page opens in order to enqueue additional scripts according to chosen template. I know how to get this information in javascript admin frontend part, however it would be more convenient if I could get it in PHP.

Share Improve this question asked Sep 4, 2020 at 9:23 LovorLovor 9969 silver badges16 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can use this code once set page template for specific post. If the template isn't set, you cant enqueue your script.

if ( $post->_wp_page_template === 'sky-template.php' ) {
        wp_enqueue_script();
}

Remember to replace sky-template.php with your template file name.

Edit:

You can get some help from this question. _wp_page_template to dynamically use template

本文标签: customizationHow to get custom page template being edited in gutenberg editor