除了创建无限数量的模板之外,在WordPress帖子中使用JavaScript的最佳实践是什么?在WordPress帖子中使用JavaScript,最佳做法是什么?
由于WordPress坚持插入p
和br
标签,因此将其放置在HTML选项卡中会导致一系列问题。任何人遇到这个问题?
预先感谢您!
除了创建无限数量的模板之外,在WordPress帖子中使用JavaScript的最佳实践是什么?在WordPress帖子中使用JavaScript,最佳做法是什么?
由于WordPress坚持插入p
和br
标签,因此将其放置在HTML选项卡中会导致一系列问题。任何人遇到这个问题?
预先感谢您!
去掉空白并返回你的JS,它会工作。这不会给WP编辑器添加任何中断和返回。
或者,在functions.php中抛出这样的:
// <!-- noformat on --> and <!-- noformat off --> functions
function newautop($text)
{
$newtext = "";
$pos = 0;
$tags = array('<!-- noformat on -->', '<!-- noformat off -->');
$status = 0;
while (!(($newpos = strpos($text, $tags[$status], $pos)) === FALSE))
{
$sub = substr($text, $pos, $newpos-$pos);
if ($status)
$newtext .= $sub;
else
$newtext .= convert_chars(wptexturize(wpautop($sub))); //Apply both functions (faster)
$pos = $newpos+strlen($tags[$status]);
$status = $status?0:1;
}
$sub = substr($text, $pos, strlen($text)-$pos);
if ($status)
$newtext .= $sub;
else
$newtext .= convert_chars(wptexturize(wpautop($sub))); //Apply both functions (faster)
//To remove the tags
$newtext = str_replace($tags[0], "", $newtext);
$newtext = str_replace($tags[1], "", $newtext);
return $newtext;
}
function newtexturize($text)
{
return $text;
}
function new_convert_chars($text)
{
return $text;
}
remove_filter('the_content', 'wpautop');
add_filter('the_content', 'newautop');
remove_filter('the_content', 'wptexturize');
add_filter('the_content', 'newtexturize');
remove_filter('the_content', 'convert_chars');
add_filter('the_content', 'new_convert_chars');
,然后你括JS在编辑器中是这样的:
<!-- noformat on -->
<script type="text/javascript">
blah blah blah
</script>
<!-- noformat off -->
有几个黑客,让你要做到这一点,但最简单的方法是使用一个插件,将允许您将javascript添加到单个帖子。 Google拉起了this one,但我相信还有更多。
或者,您可以编写一个“shell”JavaScript文件,该文件基于页面的URL将<script>
标记注入到标题中。假设您要将名为testFile
的脚本文件添加到名为testPage
的页面,并且URL为www.mydomain.com/testPage
。把下面的代码在一个名为shell.js
一个shell文件:
if (location.href === "http://www.mydomain.com/testPage") {
var testFile = document.createElement("script");
testFile.setAttribute("src","testFile.js");
document.getElementsByTagName("head")[0].appendChild(testFile);
}
你需要一个单独的脚本注入代码片段添加到shell.js
与自定义JavaScript每一页。
然后使用WordPress内置的编辑器将<script src='shell.js'></script>
添加到您的标题模板中。
WordPress还有几个official recommendations。
为什么你需要无限量的模板?为什么不把JavaScript放在你的模板里? – Brad
或者只是使用插件来修复格式? http://wordpress.org/extend/plugins/allow-javascript-in-posts-and-pages/ –