2016-01-05 85 views
0

對不起,如果我的問題是基本或愚蠢的,但請幫我解決這個問題。我試圖在wordpress中動態地更改<title><meta name="description" >標籤。所以這是我在function.php文件中試過的。如何在WordPress中替換元標題和元描述?

function changeMeta_2(){ 
    global $wpdb; 
    $cur_url = $_SERVER['REQUEST_URI']; 
    $basename = pathinfo($cur_url); 
    $ebasename = $basename['filename']; 
    if(is_numeric($ebasename)) { 
    $url = explode('/', $basename['dirname']); 
    $basename = explode('.', $url[count($url)-2]); 
    $ebasename = $basename[0]; 
    } 
    $pageName = $ebasename; 



    $arraylist_subcat = array("car","bike","boat","xxxx","yyyy","zzz","mmmm"); 
    $arraylist_maincat = array("aus","ind","usa","uae"); 


    $category_id = get_term_by('slug',$pageName, 'category'); 
    $category_parentid = get_term_by('id', $category_id->parent, 'category'); 
    $parent_slug = $category_parentid->slug; 



    if (is_page()) {   
     if (in_array($pageName,$arraylist_maincat)) {   
       $metaTitle = 'Browse '.$pageName.' | Some txt title | mysite.com'; 
       $metaDescription = 'some of custome blablaaaaa text description '.$pageName.' some of custome blablaaaaa text description ';        
       echo '<title>'.$metaTitle.'</title>'; 
       echo '<meta name="description" content="'.$metaDescription.'"/>';     
     } 
    } 
} 
add_action('wp_head', 'changeMeta_2'); 

在上面的代碼中,我試圖改變與數組值(in_array條件)匹配的術語id的標題標籤和元描述。

一切工作正常,但問題是代替覆蓋(替換)<title>標記追加頭。它沒有改變它追加。請有人幫我解決這個問題。

+1

也許你正在尋找Yoast插件? https://wordpress.org/plugins/wordpress-seo/使用該插件,您可以管理您的每頁SEO數據.. –

+0

我嘗試編碼,而不是插件。請告訴我如何使用函數中的編碼替換標題標籤php –

回答

1

對於未來有人提出這個問題:這個功能可以使用Yoast SEO plugin來完成。

然而,如果你想還是自己做....

爲了修改標題,而不是wp_head鉤,你需要使用,實際上讓你的過濾器修改標題:wp_title

而且你可以/應該按順序使用wp_head添加 meta描述(見這裏的文檔:http://codex.wordpress.org/Meta_Tags_in_WordPress

還要注意有更容易的方法來獲取頁面的標題,下文提到的...

爲標題,您的代碼會看起來像這樣:

function changeTitle($title, $sep, $seplocation){ 
    global $wpdb; 

    // NOTE: This is the HARD way to get the page title, and is unreliable... 
    $cur_url = $_SERVER['REQUEST_URI']; 
    $basename = pathinfo($cur_url); 
    $ebasename = $basename['filename']; 

    if(is_numeric($ebasename)) { 
     $url = explode('/', $basename['dirname']); 
     $basename = explode('.', $url[count($url)-2]); 
     $ebasename = $basename[0]; 
    } 

    $pageName = $ebasename; 

    // NOTE: Why not get pagename this way? 
    global $post; 
    $pageName = $post->post_title; 

    // or if you need the slug... 
    $pageName = $post->post_slug; 

    $arraylist_subcat = array("car","bike","boat","xxxx","yyyy","zzz","mmmm"); 
    $arraylist_maincat = array("aus","ind","usa","uae"); 


    $category_id = get_term_by('slug',$pageName, 'category'); 
    $category_parentid = get_term_by('id', $category_id->parent, 'category'); 
    $parent_slug = $category_parentid->slug; 



    if (is_page()) {   
     if (in_array($pageName,$arraylist_maincat)) {   
       $title = 'Browse '.$pageName.' | Some txt title | mysite.com';     
     } 
    } 

    return $title; 
} 

add_action('wp_title', 'changeTitle', 10, 3); 
+0

非常感謝你@cale_b :)現在工作:) –