在函数中。php中,我想访问数据库一次,以获取一个ACF对象,然后在本地“保存”它,以防止对数据库的进一步请求。
我想首先在“init”钩子中调用以下函数。
然后,假设,当我在以后的挂钩上调用它时,由于使用了“static”关键字,$current\\u store var已经设置好了,函数将在第一个“if”返回已经保存的静态var时停止。
它不起作用-当访问稍后挂钩上的函数时,“isset($current\\u store)”返回false。
我做错了什么?
function get_current_store()
{
if (isset($current_store)) {
return $current_store;
}
$store_url_parts = explode(\'.\', $_SERVER[\'HTTP_HOST\']);
$store_subdomain = $store_url_parts[0];
$store_url_parts_reversed = array_reverse($store_url_parts);
if (in_array("il", $store_url_parts_reversed)) {
$domain = $store_url_parts_reversed[2];
} else {
$domain = $store_url_parts_reversed[2];
}
if ($store_subdomain == $domain) {
$current_store = \'main\';
return $current_store;
}
if ($stores_page = get_page_by_title(\'Stores\', OBJECT, \'option\')) {
$stores_page_id = $stores_page->ID;
if (function_exists(\'have_rows\')) {
if (have_rows(\'stores\', $stores_page_id)) {
$store_url_parts = get_store_url_parts();
$store_subdomain = $store_url_parts[0];
$stores = array();
while (have_rows(\'stores\', $stores_page_id)) {
the_row();
$store = get_row(true);
$stores[] = $store;
}
$subdomains = array_column($stores, \'store_subdomain\');
$current_store_id = array_search($store_subdomain, $subdomains);
static $current_store = array();
if ($current_store_id !== false) {
$current_store = $stores[$current_store_id];
} else {
$current_store = false;
}
return $current_store;
}
}
}
}