我按照pat-j和cjbj的指示回答:Dynamically Override Fancy Title 我得到了覆盖标题的结果。现在我需要动态更改此标题。我编写了此函数:
add_filter (\'presscore_get_page_title\',\'netbooking_presscore_get_page_title\',10,1);
function netbooking_presscore_get_page_title ( $title ) {
if( is_page( \'1057\' ) ){
$GLOBALS["content"] = $content;
//如果出现错误,则读取输入时考虑默认值
$INPUT = function($param, $default, $prefix = true){
$key = $prefix ? \'netbooking_structure_\'.$param : $param;
return isset($GLOBALS["content"][$key]) ? $GLOBALS["content"][$key] : $default;
};
//此读取获取输入
$language = $INPUT(\'language\', \'it\', false);
$lang = substr($language, 0, 2);
$upwd = $INPUT(\'upwd\', \'8UDW37tF\', false);
$cryptedID = $INPUT(\'crypted_id\', \'Nel6LjH9xZ4=\', false);
$structure_id = $INPUT(\'sid\', 268, false);
$accomodation_id = $INPUT(\'aid\', \'4894\', false);
//调用并获取包含住宿数据的XML
$today = getdate();
$api = \'http://url\';
$query = http_build_query([
\'get\' => \'getaccomodation\',
\'upwd\' => $upwd,
\'id\' => $structure_id,
\'idacc\' => $accomodation_id,
\'type\' => \'p\',
\'mode\' => \'xml\',
\'ukey\' => \'create\',
\'l\' => $lang,
\'dstart\' => $today[\'mday\'],
\'mstart\' => $today[\'mon\'],
\'ystart\' => $today[\'year\'],
]);
$xml = new SimpleXMLElement(file_get_contents($api. \'?\' . $query));
$accomodation = $xml->accomodation;
// Ottengo l\'XML del prezzo della accomodation
$api = \'http://netbooking.naturalbooking.it/getnbdata.php\';
$query = http_build_query([
\'get\' => \'accomodationminprice\',
\'upwd\' => $upwd,
\'id\' => $structure_id,
\'idacc\' => $accomodation_id,
\'type\' => \'p\',
\'mode\' => \'xml\',
\'ukey\' => \'create\',
\'l\' => $lang,
]);
$priceData = new SimpleXMLElement(file_get_contents($api. \'?\' . $query));
//这里有当前住宿的链接
$link = \'http://url\';
$query = http_build_query([
\'cmd\' => \'bookstep1\',
\'id\' => $cryptedID,
\'type\' => \'p\',
\'l\' => $lang,
\'idacc\' => $accomodation_id
]);
$link = $link. \'?\' . $query;
//本地设置
setlocale(LC_TIME, $language);
//获取标题
$accomodation->id;
if ($accomodation){
foreach($accomodation as $accomodation) {
$title = $accomodation->name_struct." ".$accomodation->name;
}
}
}
return $title;
}
它总是返回列表中第一个住宿的名称,而不是当前住宿的名称。
EDIT我确定问题在get函数内部。但我不明白为什么页面模板中的相同内容可以工作,而在挂钩中却不起作用。这是函数的最新版本:
// ACCOMODATION TITLE
add_filter (\'presscore_get_page_title\',\'netbooking_presscore_get_page_title\',10,1); // filtro che aggiunge la funzione per sovrascrivere il title
function netbooking_presscore_get_page_title ( $title ) { // title function
if( is_page( \'1057\' ) ){ // conditional output
global $content;
$GLOBALS["content"] = $content;
// This reads input considering a default value in case of error
$INPUT = function($param, $default, $prefix = true){
$key = $prefix ? \'netbooking_structure_\'.$param : $param;
return isset($GLOBALS["content"][$key]) ? $GLOBALS["content"][$key] : $default;
};
// Read GET input
$language = $INPUT(\'language\', \'it\', false);
$lang = substr($language, 0, 2);
$upwd = $INPUT(\'upwd\', \'8UDW37tF\', false);
$cryptedID = $INPUT(\'crypted_id\', \'Nel6LjH9xZ4=\', false);
$structure_id = $INPUT(\'sid\', 268, false);
$accomodation_id = $INPUT(\'aid\', \'4894\', false);
// Build XML Accomodation
$today = getdate();
$api = \'http://myurl.it\';
$query = http_build_query([
\'get\' => \'getaccomodation\',
\'upwd\' => $upwd,
\'id\' => $structure_id,
\'idacc\' => $accomodation_id,
\'type\' => \'p\',
\'mode\' => \'xml\',
\'ukey\' => \'create\',
\'l\' => $lang,
\'dstart\' => $today[\'mday\'],
\'mstart\' => $today[\'mon\'],
\'ystart\' => $today[\'year\'],
]);
$xml = new SimpleXMLElement(file_get_contents($api. \'?\' . $query));
// Imposto il locale
setlocale(LC_TIME, $language);
$nomestruttura=(string)$xml->accomodation->name_struct;
$nome=(string)$xml->accomodation->name;
$title=$nomestruttura." ".$nome;
}
return $title;
}
最合适的回答,由SO网友:Monica Maria Crapanzano 整理而成
我终于明白了。该函数的最终版本为:
<?php
// ACCOMODATION TITLE
add_filter (\'presscore_get_page_title\',\'netbooking_presscore_get_page_title\',10,1);
function netbooking_presscore_get_page_title ( $title ) {
if( is_page( \'1057\' ) ){
$GLOBALS["content"] = $content;
// This reads input considering a default value in case of error
$INPUT = function($param, $default, $prefix = true){
$key = $prefix ? \'netbooking_structure_\'.$param : $param;
return isset($GLOBALS["content"][$key]) ? $GLOBALS["content"][$key] : $default;
};
// Read input GET
$language = $INPUT(\'language\', \'it\', false);
$lang = substr($language, 0, 2);
$upwd = $INPUT(\'upwd\', \'8UDW37tF\', false);
$cryptedID = $INPUT(\'crypted_id\', \'Nel6LjH9xZ4=\', false);
$structure_id = $INPUT(\'sid\', 268, false);
$accomodation_id = $INPUT(\'aid\', \'4894\', false);
// XML accomodation
$today = getdate();
$api = \'http://\';
$query = http_build_query([
\'get\' => \'getaccomodation\',
\'upwd\' => $upwd,
\'id\' => $structure_id,
\'idacc\' => $accomodation_id,
\'type\' => \'p\',
\'mode\' => \'xml\',
\'ukey\' => \'create\',
\'l\' => $lang,
\'dstart\' => $today[\'mday\'],
\'mstart\' => $today[\'mon\'],
\'ystart\' => $today[\'year\'],
]);
$xml = new SimpleXMLElement(file_get_contents($api. \'?\' . $query));
// Imposto il locale
setlocale(LC_TIME, $language);
$url="htp://";
$xml = simplexml_load_file($url);
$nomestruttura=(string)$xml->accomodation->name_struct;
$nome=(string)$xml->accomodation->name;
$title=$nomestruttura." ".$nome;
}
return $title;
}
?>
我理解错误在于获取xml的函数中。现在我必须解决这个问题。
EDIT & SOLVED // ACCOMODATION TITLE
add_filter (\'presscore_get_page_title\',\'netbooking_presscore_get_page_title\',10,1); // filter for title
function netbooking_presscore_get_page_title ( $title ) { // function to override title
if( is_page( icl_object_id(\'1057\')) ){ // Conditional output with ICL support
// Added Query
global $wp_query;
// Get Values
$content = [];
$content["crypted_id"] = get_option(\'netbooking_\'.\'home\'.\'_crypted_id_meta\');
$content["upwd"] = get_option(\'netbooking_\'.\'home\'.\'_upwd_meta\');
$content["structure_id"] = get_option(\'netbooking_\'.\'home\'.\'_structure_id_meta\');
$content["language"] = netbooking_get_language();
$content["sid"] = isset($_GET[\'sid\']) ? $_GET[\'sid\'] : $wp_query->query_vars[\'sid\'];
$content["aid"] = isset($_GET[\'aid\']) ? $_GET[\'aid\'] : $wp_query->query_vars[\'aid\'];
foreach ($GLOBALS[\'structure\'.\'-options\'] as $key => $option) {
$content[$key] = get_option($key);
}
$GLOBALS["content"] = $content;
// Function to read default values in case of error
$INPUT = function($param, $default, $prefix = true){
$key = $prefix ? \'netbooking_structure_\'.$param : $param;
return isset($GLOBALS["content"][$key]) ? $GLOBALS["content"][$key] : $default;
};
// GET Input
$language = $INPUT(\'language\', \'it\', false);
$lang = substr($language, 0, 2);
$upwd = $INPUT(\'upwd\', \'8UDW37tF\', false);
$cryptedID = $INPUT(\'crypted_id\', \'Nel6LjH9xZ4=\', false);
$structure_id = $INPUT(\'sid\', 268, false);
$accomodation_id = $INPUT(\'aid\', \'4894\', false);
// Accomodation XML
$today = getdate();
$api = \'http://myurl.it\';
$query = http_build_query([
\'get\' => \'getaccomodation\',
\'upwd\' => $upwd,
\'id\' => $structure_id,
\'idacc\' => $accomodation_id,
\'type\' => \'p\',
\'mode\' => \'xml\',
\'ukey\' => \'create\',
\'l\' => $lang,
\'dstart\' => $today[\'mday\'],
\'mstart\' => $today[\'mon\'],
\'ystart\' => $today[\'year\'],
]);
$xml = new SimpleXMLElement(file_get_contents($api. \'?\' . $query));
// Imposto il locale
setlocale(LC_TIME, $language);
$nomestruttura=(string)$xml->accomodation->name_struct;
$nome=(string)$xml->accomodation->name;
$title=$nomestruttura." ".$nome;
}
return $title;
}
这终于奏效了!