對不起,如果我的問題是基本或愚蠢的,但請幫我解決這個問題。我試圖在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>
標記追加頭。它沒有改變它追加。請有人幫我解決這個問題。
也許你正在尋找Yoast插件? https://wordpress.org/plugins/wordpress-seo/使用該插件,您可以管理您的每頁SEO數據.. –
我嘗試編碼,而不是插件。請告訴我如何使用函數中的編碼替換標題標籤php –