如何在javascript中获取wordpress主页url?

如何在javascript中获取wordpress主页url?,javascript,wordpress,Javascript,Wordpress,在php中get_home_url() 我正在使用ajax <script> $j=jQuery.noConflict(); $j(document).ready(function(){ $j('.new_post_btn').click(function(){ var dataString = { 'action': 'new_post_action', 'post_status': '

在php中get_home_url() 我正在使用ajax

<script>
$j=jQuery.noConflict();
    $j(document).ready(function(){
        $j('.new_post_btn').click(function(){
            var dataString = {
            'action': 'new_post_action',
            'post_status': 'draft',
        };

            dataString=
                jQuery.ajax({
                    type: "POST",
    url: "http://localhost/wordpress/wp-admin/admin-ajax.php",
                  data: dataString,
                                    success: function(msg){
                         if(msg!='false')
                                                 {
                                                     msg=msg.substring(0, msg.length-1);
                                                     window.location='http://localhost/wordpress/wp-admin/post.php?post='+msg+'&action=elementor';
                                                 }
                       }

                 });
        });
});
</script>

$j=jQuery.noConflict();
$j(文档).ready(函数(){
$j('.new\u post\u btn')。单击(函数(){
变量数据字符串={
“行动”:新的行动后的行动,
“后期状态”:“草稿”,
};
数据串=
jQuery.ajax({
类型:“POST”,
url:“http://localhost/wordpress/wp-admin/admin-ajax.php",
数据:dataString,
成功:功能(msg){
如果(消息!='false')
{
msg=msg.substring(0,msg.length-1);
窗口位置http://localhost/wordpress/wp-admin/post.php?post=“+msg+”&action=elementor';
}
}
});
});
});

我想使用“”到javascript函数获取wordpress主页url。

如果您正在寻找查找当前页面主机的方法,请使用
document.location

console.log(“document.location.href:+document.location.href”);
console.log(“document.location.origin:+document.location.origin”);
log(“document.location.hostname:+document.location.hostname”);
//-->如果要传递另一个url以获取域名,则可以使用以下函数。
var tmp=document.createElement('a');
tmp.href=”http://www.example.com/my-page";
console.log(“主机:+tmp.host”);

log(“主机:+tmp.hostname”)您可以从WP-JSON链接标签获取Wordpress的主页Url,如下所示:

// get JSON url
var WpJsonUrl = document.querySelector('link[rel="https://api.w.org/"]').href
// then take out the '/wp-json/' part
var homeurl = WpJsonUrl.replace('/wp-json/','');

你的答案的问题是它没有考虑Wordpress安装的目录。它可能是“website.com/blog/”而不是“website.com”。在这种情况下,你的答案是错误的。