我在http://example.com
安装了Wordpress,并在目录(http://example.com/my_dir_X
)中安装了许多其他Wordpress。
我怎么能在PHP中知道我在哪个目录?
答案 0 :(得分:1)
只需使用此代码,它就会在前端
上回显它<?php echo site_url(); ?>
或者在仪表板管理栏中回显你可以在functions.php
中尝试这样的事情// add links/menus to the admin bar
function mytheme_admin_bar_render() {
global $wp_admin_bar;
$wp_admin_bar->add_menu( array(
'parent' => 'new-content', // use 'false' for a root menu, or pass the ID of the parent menu
'id' => 'new_media', // link ID, defaults to a sanitized title value
'title' => site_url(), // link title
'href' => echo site_url(), // name of file
'meta' => false // array of any of the following options: array( 'html'
=> '', 'class' => '', 'onclick' => '', target => '', title => '' );
));
}
add_action( 'wp_before_admin_bar_render', 'mytheme_admin_bar_render' );
答案 1 :(得分:0)
使用此功能:
<?php echo get_site_url(); ?>
或者:
<?php echo home_url(); ?>
您可以在此处详细了解get_site_url()函数:https://developer.wordpress.org/reference/functions/get_site_url/
关于home_url(): https://codex.wordpress.org/Function_Reference/home_url
建议使用get_site_url()。
答案 2 :(得分:0)
根据你给别人的答案,这是我提出的解决方案。
<?php
// Just to show you how it works, I'm overwriting the $_SERVER values, in your case you can directly use the values from $_SERVER
$_SERVER['SERVER_NAME'] = 'example.com';
$_SERVER['DOCUMENT_ROOT'] = '/home/ab454567/public_html/example.com/my_dir_X/abc';
$domain = $_SERVER['SERVER_NAME'];
$fullPath = $_SERVER['DOCUMENT_ROOT'];
preg_match('#^(.*)(/' . $domain . ')(?P<myDir>.*)#i', $fullPath, $matches);
if(isset($matches['myDir']) && !empty($matches['myDir'])) {
echo "Found the root directory: " . $matches['myDir'] . PHP_EOL;
}
else {
echo "No root dir found, probably I'm in the public_html root directory of the domain." . PHP_EOL;
}
// Output is:
// Found the root directory: /my_dir_X/abc
这是你的情况,对吗?