2015-02-05 119 views
0

我有一个插件,用word替换文本{headernav}例如,当wordpress页面加载。它在页面首次加载时工作正常,但是如果我导航到另一个页面,{headernav}将显示而不是由javascript替换的html内容。执行JavaScript后点击wordpress永久链接不工作

我试过的东西。

  1. 我用萤火并发现,当我点击了永久链接,并将其转到页后的javscript被加载。

下面是插件加载的JavaScript代码。

的Javascript:

jQuery(document).ready(function($){//begin document ready function call 
     //place needed javascript for the plugin elements inside this function call 


    $.get("wp-content/plugins/elemental/content/headernav.php",function(element){ 

//the tag to replace 
var headernav = "{headernav}"; 

//create instance of RegExp 
var regEx = new RegExp(headernav,"g"); 

//get the html of the body 
var html = $("body").html(); 

//replace the {headernav} with the html returned by the get method 
var newValue = html.replace(regEx,element); 

//update the body with the new html value 
$("body").html(newValue); 


}); 

//do not place any javascript code on or past this line 
});//end document ready function call 
+0

所以这个插件加载在永久链接去的页面上呢?通过说{nav}显示,你的意思是{headernav}?另一件事是 – Xeschylus 2015-02-05 13:26:54

+0

。您使用$ .get()的相对路径。你是否和你的第一页在同一根?如果您从example.com转到example.com/post,那么相关链接将被打破,从而导致错误。这又会导致$ .get()失败。 – Xeschylus 2015-02-05 13:30:19

+0

是的,我的意思是{headernav},我忘了编辑。点击固定链接后,您的地址栏中显示的路径是正确的。 http:// localhost/wordpress_dev/adding-an-image-test /你有任何关于在我的JavaScript文件中使用相对链接的建议吗? – 2015-02-05 13:46:01

回答

1

使用wp_localize_script传递变量到你的插件。也可以使用Wordpress内置的ajax功能。看看this article

+0

这个技巧(wp_localize_script)。我会研究一下你的内置ajax建议。谢谢,拉里 – 2015-02-05 21:02:29

0

根据我的评论,它似乎是一个问题与您的$ .get()路径,这将产生404错误,并不执行代码。

绝对路径或配置文件通常通过添加足够的A /路径中的前面,所以/wp-content/plugins/elemental/content/headernav.php

这将在启动的JavaScript你的域名根(www.example.com)然后从那里建立到你想要的路径。

为了确保它只在你的域中,我通常也为根添加一个配置文件,而不仅仅依赖于使用绝对路径。

希望有所帮助。