[英]Making an ajax call from a file that's included in many pages
我正在從各個頁面中包含的文件中進行簡單的調用。
$.post( "wp-content/themes/biagetti_studio/inc/HandleBackground.php", {color:color} );
問題是我認為該調用是相對於包含js的文件的路徑(所有包含js的文件都在同一目錄中),但是我發現該調用相對於當前路徑,因此如果我在localhost/biagettistudio
那么腳本調用localhost/biagettistudio/wp-content/themes/biagetti_studio/inc/HandleBackground.php
但是如果我在localhost/biagettistudio/projects
則調用localhost/biagettistudio/projects/wp-content/themes/biagetti_studio/inc/HandleBackground.php
。
我知道我可以走絕對的道路
$.post( "/wp-content/themes/biagetti_studio/inc/HandleBackground.php", {color:color} );
但這提出了一個問題,如果我必須移植站點,而基本目錄不是根目錄。 我顯然在這里想念什么?
你可以用
<script type="text/javascript">
<?php
echo "var theme_dir ='" . get_theme_root() . ‘/’ . get_template() ."';";
?>
</script>
在footer.php中(因此它在每個頁面上都可用),如果您移植站點,則不必擔心會破壞內容。
然后您可以像這樣調用腳本...
$.post( theme_dir + "/biagetti_studio/inc/HandleBackground.php", {color:color} );
這個怎么樣:
var path = location.pathname.match(/(.*)\/wp-content\/.*/)[1];
$.post( path + "/wp-content/themes/biagetti_studio/inc/HandleBackground.php", {color:color} );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.