作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在这里找到了这个很棒的教程:codrops
但我不知道如何让它在 WordPress 中工作!我是 Javascript 的初学者/中级,但我真的不知道如何将其插入 Wordpress。 Wordpress Codex 提到将脚本直接放入帖子中,因为它不会在站点范围内使用。有没有人成功地让它发挥作用?你能给我指导如何做吗?
最佳答案
这是您可以做到的一种方法。通过在functions.php文件中创建一个简短的代码,您可以包含您需要的js资源。请注意,对于此实现,您必须包含此行中定义的本地资源:element.src = '/wp-content/themes/custom_name_space/js/$src';
或者您可以使用某种插件来将自定义 js 添加到帖子中。
/** you will need to include this invocation in the source file. */
$(function() {
Grid.init();
});
<?php add_shortcode( 'custom_name_space_addJs', function ( $atts ) {
/**
* create a javascript shortcode
* shortcode takes an id attribute and src attribute
* leaving src attribute blank will generate a 404 Error Code
*/
extract( shortcode_atts( array(
'id'=>'js-custom_name_space-script', 'src' => 'no-script.js', ), $atts ) ); return "
<script id='js-add-script-element'>
(function(w, doc) {
'use strict';
function downloadJSAtOnload() {
var element = doc.createElement('script');
element.id = '$id';
element.src = '/wp-content/themes/custom_name_space/js/$src';
doc.body.appendChild(element);
}
/* Check for browser support of event handling capability */
if (w.addEventListener) {
w.addEventListener('load', downloadJSAtOnload, false);
} else if (w.attachEvent) {
w.attachEvent('onload', downloadJSAtOnload);
} else {
w.onload = downloadJSAtOnload;
}
}(window, document));
</script>"; }); ?>
<!-- Then inside a wordpress post, write the needed HTML from the tutorial and use the short code -->
[custom_name_space_addJs id="thumb-grid" src="grid.js"]
关于javascript - WordPress 缩略图网格扩展预览,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30903843/
我是一名优秀的程序员,十分优秀!