如何在JavaScript中获取wordpress主页?

时间:2018-05-15 01:32:09

标签: 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': '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>

我想使用'http://localhost/wordpress'到javascript函数来获取wordpress主页。

2 个答案:

答案 0 :(得分:3)

您可以从WP-JSON链接标记中获取Wordpress的主页,如下所示:

// 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/','');

答案 1 :(得分:0)

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

&#13;
&#13;
console.log("document.location.href : "+ document.location.href);
console.log("document.location.origin : "+ document.location.origin);
console.log("document.location.hostname : "+ document.location.hostname);

 // --> If you want to pass another url to fetch domain home name then you can use following function.


var tmp = document.createElement('a');
tmp.href   = "http://www.example.com/my-page";
console.log("host : "+tmp.host);
console.log("Host : "+tmp.hostname);
&#13;
&#13;
&#13;