最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Add CSS and JS files of the template post on a WordPress page

programmeradmin6浏览0评论

Out of necessity I should add all the CSS and JS files present in any WordPress article on a specific page through code.

Do you know how to add all these files?

If I try through the normal get_header() code, the necessary files are added for the page and not for the articles, as it should normally be.

I would like to make this change because I embed an article on a page and the codes of a plugin are not presented if the page is not actually read as an article.

Thanks a lot to those who will help me.

This is the answer I get if I try to add files after files, that is, all CSS and JS

Out of necessity I should add all the CSS and JS files present in any WordPress article on a specific page through code.

Do you know how to add all these files?

If I try through the normal get_header() code, the necessary files are added for the page and not for the articles, as it should normally be.

I would like to make this change because I embed an article on a page and the codes of a plugin are not presented if the page is not actually read as an article.

Thanks a lot to those who will help me.

This is the answer I get if I try to add files after files, that is, all CSS and JS

Share Improve this question edited Jan 16, 2020 at 16:45 Matteo Feduzi asked Jan 16, 2020 at 14:03 Matteo FeduziMatteo Feduzi 291 silver badge9 bronze badges 3
  • So the page you're mentioning is not a page created a wp page but an extra page with a physical file? otherwise, if it's a wp page, the .js and .css files should be enqueued, except if some plugins are enqueuing scripts and styles only for specific post_types.. – Andrea Somovigo Commented Jan 16, 2020 at 14:59
  • Surely the plugin in question queues for post_types. How can I do? And anyway yes, my pages are all personalized but they use the basic WordPress code. – Matteo Feduzi Commented Jan 16, 2020 at 15:24
  • the screenshot shows js errors that can depend on other issues, check the source code of your page and see in <head> tag if the needed styles and scripts are present. In this case the enqueuing is ok but the errors are caused by the .js themselves. CodeMirror is not defined is likely to means that a some more scripts are missing. Also important the order with wich you enqueue the scripts. CodeMirror evidently must be defined before it can be used – Andrea Somovigo Commented Jan 16, 2020 at 16:49
Add a comment  | 

1 Answer 1

Reset to default 0

In your theme's file functions.php:

add_action('wp_enqueue_scripts','STACK_356524_add_css_js_on_page',99);
function STACK_356524_add_css_js_on_page(){
  global $post;
  if($post->post_type=='page') {  // check if the current wp object is a `page` also is_page() can be used: see https://codex.wordpress/Conditional_Tags 
    wp_enqueue_script( 'js_handler', 'url_of_your_script', array(), '' );
    wp_enqueue_style('css_handler','url_of_your_style',array(), '1.0.0', 'all');
  }
}

Up to you adjust the correct path to the .css and .js you need to enqueue, you can have a look at the source page where the scripts and files are present and use that string. More granular adjustment can be done ( versioning, dependencies -I.E. jquery or even enqueue only on specific pages -i.e: if( post->ID ==12345){}.

For more info:

https://developer.wordpress/reference/functions/wp_enqueue_script/ https://developer.wordpress/reference/functions/wp_enqueue_style/

(IT) https://softrade.it/WP/wp_enqueue_script/

发布评论

评论列表(0)

  1. 暂无评论