2015-05-28 83 views
1

我需要用自定义元描述替换由header.php中wp_head()函数生成的现有<meta name="description"...>。页面中的信息不是一个普通的WordPress的帖子,来自外部的数据库。 我可以加我的自定义元,但是旧的也有Wordpress替换元描述

function add_meta_tags() 
{ 
    global $data; 
    if(!is_null($data['metas']['page_meta_description'])) 
    { 
     echo '<meta name="description" content="'.$data['metas']['page_meta_description'].'">'; 
    } 
} 
add_action('wp_head', 'add_meta_tags'); 

有没有什么办法: - 删除与 function.php一个动作或过滤器的默认元描述?或者, - 在呈现之前以某种方式替换元描述的值?

回答

2

描述元标记通常由模板标题(header.php)或通过将描述添加到网站(例如SEO标题标记)的插件来处理。既然你得到了重复的描述,你应该检查输出描述标签的插件。

对于其他恼人的元标记和其他东西放在头,你可以使用remove_action()函数模板的functions.php文件来做到这一点,可以看看这里的文档:https://codex.wordpress.org/Function_Reference/remove_action

我为我运行的WP网站做了类似的事情,需要删除每个进入头部的元标记,这里是我的functions.php文件底部的代码:

// Remove Meta Tags that are Unneeded 
remove_action('wp_head','feed_links_extra', 3); 
remove_action('wp_head','rsd_link'); 
remove_action('wp_head','feed_links', 2); 
remove_action('wp_head','wlwmanifest_link'); 
remove_action('wp_head','index_rel_link'); 
remove_action('wp_head','parent_post_rel_link', 10, 0); 
remove_action('wp_head','start_post_rel_link', 10, 0); 
remove_action('wp_head','adjacent_posts_rel_link', 10, 0); 
remove_action('wp_head','noindex'); 
remove_action('wp_head','wp_generator'); 
remove_action('wp_head','rel_canonical'); 
remove_action('wp_head', 'wp_shortlink_wp_head'); 

显然,只使用你需要的!我无法找到meta标签所有功能的列表,所以我想包括所有我使用的功能。

+0

这似乎是我正在寻找的,但我是一个菜鸟,当涉及到WP。我需要寻找负责元描述的操作。 – Scarpelius

+0

还检查您的插件的任何输出不需要的元标记 - 搜索引擎优化标题标签和其他人会做到这一点。另外,这个模板也可能在header.php – Jack

+0

中这样做,我解决了你的问题 - 我忘记了描述是非常少的事情之一。 – Jack

1

安装Yoast插件,您将能够通过手动将其标记为您想要显示在搜索引擎结果页上的元标记。

0
function remove_meta_descriptions($html) { 
    $pattern = '/<meta name(.*)=(.*)"description"(.*)>/i'; 
    $html = preg_replace($pattern, '', $html); 
    return $html; 
} 
function clean_meta_descriptions($html) { 
    ob_start('remove_meta_descriptions'); 
} 
add_action('get_header', 'clean_meta_descriptions', 100); 
add_action('wp_footer', function(){ ob_end_flush(); }, 100);