如何获得主页url


How to get the homepage url?

我在Wordpress上做一个重定向页面。PHP将返回到网站首页。但是我找不到主页的url了。

我的php文件路径xampp'htdocs'wordpress'return.php

下面是我的代码:

       $url = "$_SERVER[HTTP_HOST]";
       header('Refresh: 3;url=' . $url);
       echo get_option('home');
       echo $url;

$urllocalhost8080/wordpess/return.php。我想从url : localhost8080/wordpess/return.php转到url : local:8080/wordpress

我怎么能得到url local:8080/wordpress ?

Thx

Wordpress有一个内置函数:wp_redirect(见doc)

require_once( dirname(__FILE__) . '/wp-load.php' ); // first you need to load WordPress libraries if you are in an external file.
wp_redirect( home_url() );
exit; // don't forget to exit after a redirection

据我所知,您试图使用-

将您的页面从localhost:8080/wordpess/return.php重定向到localhost:8080/wordpess/
$url = "$_SERVER[HTTP_HOST]";
header('Refresh: 3;url=' . $url);

你需要做的是改变你的$url变量到你想要重定向的位置,也就是-

$url = "http://localhost:8080/wordpess/";
header('Refresh: 3; url =' . $url);

希望这就是你要找的。

编辑 -

如果您不想硬编码URL,您可以尝试以下操作-

$url = "/wordpess";
header("Refresh: 3; url = http://" . $_SERVER['HTTP_HOST'] . $url);

从我对你的问题的理解来看,你想从当前页面回到一个级别。就这样吗?

如果是,您可以通过做一些字符串操作来实现,如下所示:

<?php
    // Given that your current url is in the '$url' var
    $url = 'localhost8080/wordpess/return.php';
    // Find the position of the last forward slash
    $pos = strrpos($url, '/');
    // Get a substring of $url starting at position 0 to $pos 
    // (if you want to include the slash, add 1 to the position)
    $new_url = substr($url, 0, $pos + 1);
    // Then you can have the redirection code using the $new_url variable
    ...

如果我误解了,请让我知道。
希望能有所帮助。欢呼。