admin管理员组文章数量:1024615
I want to change in my search result the title. Unfortunately I don't have option to change this in , because I must do it in plugin file.
Actually I have that code in PHP file:
if (isset($_GET['search_region']) ) {
if (isset($_GET['search_region']) ) {
foreach ($cat as $catid => $categories) {
foreach ($city as $idRegion => $region) {
if ($_GET['search_categories'] == $catid && $_GET['search_region'] == $idRegion) {
printf('<h1 class="cat-title">%s %s</h1>', $categories, $region);
**
?>
<script type="text/javascript">
document.title = "<?php printf('%s %s', $categories, $region); ?>";
</script>
<?php
**
}
}
}
}
}
echo '</div>';
Everything is good, bad is only this what is between **. It would be fine but it only changing title on page bar, in the page code is unchanged. Do you have any idea how I can changing title in in this space?
I want to change in my search result the title. Unfortunately I don't have option to change this in , because I must do it in plugin file.
Actually I have that code in PHP file:
if (isset($_GET['search_region']) ) {
if (isset($_GET['search_region']) ) {
foreach ($cat as $catid => $categories) {
foreach ($city as $idRegion => $region) {
if ($_GET['search_categories'] == $catid && $_GET['search_region'] == $idRegion) {
printf('<h1 class="cat-title">%s %s</h1>', $categories, $region);
**
?>
<script type="text/javascript">
document.title = "<?php printf('%s %s', $categories, $region); ?>";
</script>
<?php
**
}
}
}
}
}
echo '</div>';
Everything is good, bad is only this what is between **. It would be fine but it only changing title on page bar, in the page code is unchanged. Do you have any idea how I can changing title in in this space?
Share Improve this question asked Apr 9, 2019 at 11:03 KacperKacper 11 bronze badge 2 |1 Answer
Reset to default 1To change document title use document_title_parts
.filter. You want to apply changes only on the search page (as indicated in the title), so you should use is_search()
conditional tag (or is_archive()
as you suggest in the comment) and your code from php file.
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;
}
I don't know your code so I may be wrong, but code that determine document name I would move to a function. Being in one place, it will be easier to manage.. Thanks to the static variable, the code will be executed only once, and subsequent function calls will return the previously determined title.
Function se333874_prepare_title
you use in se333744_site_title
and in PHP file.
functions.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 file:
if ( isset($_GET['search_region'], $_GET['search_categories']) )
printf('<h1 class="cat-title">%s %s</h1>', $categories, $region);
I want to change in my search result the title. Unfortunately I don't have option to change this in , because I must do it in plugin file.
Actually I have that code in PHP file:
if (isset($_GET['search_region']) ) {
if (isset($_GET['search_region']) ) {
foreach ($cat as $catid => $categories) {
foreach ($city as $idRegion => $region) {
if ($_GET['search_categories'] == $catid && $_GET['search_region'] == $idRegion) {
printf('<h1 class="cat-title">%s %s</h1>', $categories, $region);
**
?>
<script type="text/javascript">
document.title = "<?php printf('%s %s', $categories, $region); ?>";
</script>
<?php
**
}
}
}
}
}
echo '</div>';
Everything is good, bad is only this what is between **. It would be fine but it only changing title on page bar, in the page code is unchanged. Do you have any idea how I can changing title in in this space?
I want to change in my search result the title. Unfortunately I don't have option to change this in , because I must do it in plugin file.
Actually I have that code in PHP file:
if (isset($_GET['search_region']) ) {
if (isset($_GET['search_region']) ) {
foreach ($cat as $catid => $categories) {
foreach ($city as $idRegion => $region) {
if ($_GET['search_categories'] == $catid && $_GET['search_region'] == $idRegion) {
printf('<h1 class="cat-title">%s %s</h1>', $categories, $region);
**
?>
<script type="text/javascript">
document.title = "<?php printf('%s %s', $categories, $region); ?>";
</script>
<?php
**
}
}
}
}
}
echo '</div>';
Everything is good, bad is only this what is between **. It would be fine but it only changing title on page bar, in the page code is unchanged. Do you have any idea how I can changing title in in this space?
Share Improve this question asked Apr 9, 2019 at 11:03 KacperKacper 11 bronze badge 2-
This should work for you. You just have to change the IF condition and use
is_search()
in it. – nmr Commented Apr 9, 2019 at 12:13 - @nmr can you try implemented this in my code and write in question? please. In addition I inform, that this code I have in is_archive() – Kacper Commented Apr 9, 2019 at 12:25
1 Answer
Reset to default 1To change document title use document_title_parts
.filter. You want to apply changes only on the search page (as indicated in the title), so you should use is_search()
conditional tag (or is_archive()
as you suggest in the comment) and your code from php file.
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;
}
I don't know your code so I may be wrong, but code that determine document name I would move to a function. Being in one place, it will be easier to manage.. Thanks to the static variable, the code will be executed only once, and subsequent function calls will return the previously determined title.
Function se333874_prepare_title
you use in se333744_site_title
and in PHP file.
functions.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 file:
if ( isset($_GET['search_region'], $_GET['search_categories']) )
printf('<h1 class="cat-title">%s %s</h1>', $categories, $region);
本文标签: phpchange title page on search result
版权声明:本文标题:php - change title page on search result 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745609817a2158937.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
is_search()
in it. – nmr Commented Apr 9, 2019 at 12:13