0
votes

I have a page (Search) in WordPress, and I want to change title and meta description for this page dynamically, because this page will show posts dynamically by search term that uses have interred. I have no idea how I should do that. because I want it for a specific page only and not all pages or posts. I'd appreciate if someone helps me with this. it is important for me to solve it! I have searched and find something at this link but I don't know how should I find and call the search page. I write this, it is very basic:

    function changetitle(){

    //get some data for title
    if($_GET['_sft_category'])
       $mycategory= str_replace('-', ' ',$_GET['_sft_category']);
       if($_GET['_sft_location'])
    $mylocation=str_replace('-', ' ', $_GET['_sft_location']);

   if ( is_page(548)) {                
                $title = $mycategory.$mylocation.' | site name ';   
    }
    return $title;
}
add_action( 'wp_title', 'changetitle',10,3);

///change meta description
function changeMeta(){

   if ( is_page(548)) {                
                $metadescription = 'some content';  
    }
    return $metadescription;
}
add_action( 'wp_head', 'changeMeta');
1
please provide code snippet you had tried so we can help with that - raju_odi
@raju_eww I put the code, it is not complete and i don't know how i should complete it, please help about it. - Somy J
There will be id of search page so please paste that id in is_page(serach_page_id_here) - raju_odi
@raju_eww I put the page id but this code doesn't do anything. and title and meta description are unchanged. - Somy J
are you using <title><?php wp_title(); ?></title> in your head section? - raju_odi

1 Answers

0
votes

I solved the problem, I write my solution maybe useful for others. since I hadn't <title><?php wp_title(); ?> at my header.php, I inserted that to the file and then my custom title added to the page. for removing duplicated title tag I used remove_action( 'wp_head', '_wp_render_title_tag', 1 ); for meta description also I used meta tag at header.php but my problem is that it show meta description after some script files. is there a way to show it after title tag?