javascript - How to load external .js to the body of a Wordpress Page?
问题描述
I would like to add this script to a PAGE on wordpress theme. I don't want it at header or footer, i want it in the body of the specific page i created in Wordpress.
This code works if it was in a normal html page on a folder where Wordpress is not installed. However, if i put it in Wordpress page, the .js file doesn't seems to start up.
I know there are ways to add .js to wordpress header and footer, but i want it in the body of a page. How do i do that?
<script src="<?php bloginfo('template_directory'); ?>/js/googleformstyler.js"
form="https://docs.google.com/forms/d/e/1FAIpQLSeJZ5Es893O4ckhi8-aTOZQe4js9_q2DO1I4pBHT_ENXIpyXg/viewform?embedded=true">
</script>
解决方案
为什么你不想在页眉/页脚中加载它?下面的摘录来自下面提供的链接。使用以下代码对脚本进行排队是 wordpress 的首选方法。
您可以使用wp_enqueue_script()
.
wp_enqueue_script(
$handle,
$src = '',
$deps = array(),
$ver = false,
$in_footer = false );
参考: https ://developer.wordpress.org/reference/functions/wp_enqueue_script/
如果尚未包含脚本并且所有依赖项都已注册,则根据脚本依赖项在正确的时间将脚本文件链接到生成的页面。您可以将脚本与先前使用该
wp_register_script()
函数注册的句柄链接,或者为该函数提供链接脚本所需的所有参数。 这是将 JavaScript 链接到 WordPress 生成页面的推荐方法。
这个答案有点通用,因为如果没有来自该 .js 文件的更多信息/代码,很难知道您要实现的目标。请随时更新您的问题,在此处发表评论,社区可能会进一步帮助您进行查询。
推荐阅读
- java - 连接规范内的表
- c# - 是否可以在特定消息 ID 后收到电子邮件
- python - 编写正则表达式以查找字符串的开头和结尾是否有特殊字符,(忽略空格)
- xamarin.forms - Xamarin Syncfusion 图表不显示来自本地数据库的绑定数据
- node.js - Google Cloud App Engine - req.user 未定义
- python - 无法导入 tensorflow - 无法加载本机 tensorflow 运行时
- c# - SMTP HTML 电子邮件未通过 Gmail 电子邮件客户端呈现 HTML
- json - 在 Python 3.6 JSON 模块中,为什么我必须同时使用加载和加载?
- python-3.x - 自动输入命令提示符
- python - 大于 RAM 的 Numpy 数组:写入磁盘还是核外解决方案?