要更改文档标题,请使用document_title_parts
.滤器您只想在搜索页面上应用更改(如标题所示),因此应该使用is_search()
conditional tag (或is_archive()
正如您在注释中所建议的)和php文件中的代码。
add_filter( \'document_title_parts\', \'se333744_site_title\' );
function se333744_site_title( $title )
{
global $wp_query;
if ( is_archive() )
{
// your code here
if ( isset($_GET[\'search_region\'], $_GET[\'search_categories\']) )
{
// set variables: $cat, $city
//
foreach ($cat as $catid => $categories) {
foreach ($city as $idRegion => $region) {
if ( $catid == $_GET[\'search_categories\'] &&
$idRegion == $_GET[\'search_region\'])
{
$title[\'title\'] = sprintf("%s %s", $categories, $region);
return $title;
}
}
}
}
}
return $title;
}
我不知道你的代码,所以我可能错了,但决定文档名称的代码我会移到函数。在一个地方,管理起来会更容易。。由于使用了静态变量,代码将只执行一次,随后的函数调用将返回先前确定的标题。作用
se333874_prepare_title
您在中使用
se333744_site_title
和PHP文件中。
功能。php
add_filter( \'document_title_parts\', \'se333744_site_title\' );
function se333744_site_title( $title )
{
global $wp_query;
if ( is_search() )
{
if ( isset($_GET[\'search_region\'], $_GET[\'search_categories\']) )
$title[\'title\'] = se333874_prepare_title();
}
return $title;
}
function se333874_prepare_title()
{
static $title = null;
if ( $title != null)
return $title;
$title = false;
if ( isset($_GET[\'search_region\'], $_GET[\'search_categories\']) )
{
// set $cat and $city variables here
//
foreach ($cat as $catid => $categories) {
foreach ($city as $idRegion => $region) {
if ( $catid == $_GET[\'search_categories\'] &&
$idRegion == $_GET[\'search_region\'])
{
$title = sprintf("%s %s", $categories, $region);
return $title;
}
}
}
}
return $title;
}
PHP文件:
if ( isset($_GET[\'search_region\'], $_GET[\'search_categories\']) )
printf(\'<h1 class="cat-title">%s %s</h1>\', $categories, $region);