如何在wordpress中替换元标题和元描述? [英] How to replace meta title and meta description in wordpress?
问题描述
对不起,如果我的问题是基本的或愚蠢的,但请帮助我解决这个问题.我正在尝试在 wordpress 中动态更改
和 标签.所以这就是我在 function.php 文件中尝试的.
Sorry if my question was basic or stupid but please help me to solve this issue. I'm trying to change <title>
and <meta name="description" >
tags dynamically in wordpress. so this is what I tried in function.php file.
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 的标题标签和元描述.
In the above code I'm trying to change the title tag and meta description for term id which are matching with array values (in_array condition).
一切正常,但问题是不是覆盖(替换)<title>
标签附加在头部.它不会改变它附加.请有人帮我解决这个问题.
Everything works fine, but problem is instead of override(replace) <title>
tag is appending in head. Its not changing it appending. please someone help me to solve this issue.
推荐答案
对于将来遇到此问题的任何人:此功能可以使用 Yoast SEO 插件.
For anybody coming to this question in the future: This functionality can be accomplished using the Yoast SEO plugin.
但是,如果您仍然想自己做....
However, if you do want to still do this yourself....
为了修改标题,而不是 wp_head
钩子,您需要使用实际上允许您修改标题的过滤器:wp_title
In order to modify the title, rather than the wp_head
hook, you need to be using the filters that actually allow you to modify the title: wp_title
并且您可以/应该使用 wp_head
来添加元描述(请参阅此处的文档:http://codex.wordpress.org/Meta_Tags_in_WordPress)
And you can / should use the wp_head
in order to add the meta description (see the docs here: http://codex.wordpress.org/Meta_Tags_in_WordPress)
另请注意有更简单的方法来获取页面标题,如下所述...
Also note there's easier ways to get the page title, mentioned below...
对于标题,您的代码如下所示:
For the title, your code would look something like so:
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 );
这篇关于如何在wordpress中替换元标题和元描述?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!