简体   繁体   English

如何将自动大小脚本添加到Wordpress?

[英]How to add Autosize script to Wordpress?

I am still learning Wordpress so forgive me if this is a stupid question but I am trying to add the Autosize script to a page on my website and I am kind of lost. 我仍然在学习Wordpress,因此如果这是一个愚蠢的问题,请原谅我,但是我试图将Autosize脚本添加到我网站上的页面上,我有点迷失了。 How do you add scripts to Wordpress? 如何将脚本添加到Wordpress? And how do I call it on a specific page with the textarea element that I want to autosize? 以及如何在具有要自动调整大小的textarea元素的特定页面上调用它?

To include the scripts to WordPress you need to use wp_enqueue_script Documentation Since you will need to call the JS to target the textarea , use the function in a custom.js file and include that too. 要将脚本包括到WordPress中,您需要使用wp_enqueue_script 文档。由于您将需要调用JS来定位textarea ,因此请在custom.js文件中使用该函数并将其包括在内。

function autoresize_scripts() {
  wp_enqueue_script( 'auto-resize', get_template_directory_uri() . '/js/autoresize.js', array(), '1.0.0', true );
  wp_enqueue_script( 'custom-js', get_template_directory_uri() . '/js/custom.js', array(), '1.0.0', true );
}

add_action( 'wp_enqueue_scripts', 'autoresize_scripts' );

The above code will be used in functions.php 上面的代码将在functions.php

If you need to call it on a specific page: 如果需要在特定页面上调用它:

Use is_page function in WordPress: 在WordPress中使用is_page函数:

if( is_page( 2 ) ) // 2 is the page id
{
    // Target textarea with JS
}

And if you want to target many pages with the textarea element, WordPress will automatically take care as it is added to footer.php which is loaded in all the web pages. 而且,如果您希望使用textarea元素定位许多页面,则WordPress将自动注意将其添加到在所有网页中加载的footer.php中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM