WordPress 替换元描述
Wordpress replace meta description
我需要用自定义元描述替换 header.php 中 wp_head() 函数生成的现有 <meta name="description"...>
。页面中的信息不是常规的 wordpress post,是从外部数据库中获取的。
我能够添加我的自定义元数据,但旧的元数据也在那里
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?要么,
- 在呈现之前以某种方式替换元描述的值?
Description Meta 标签通常由模板 header (header.php) 或向网站添加描述的插件处理(例如 SEO 标题标签)。由于您得到了重复的描述,您应该检查输出描述标签的插件。
对于放在 header 中的其他烦人的元标记和其他内容,您可以使用模板 functions.php 文件中的 remove_action() 函数来执行此操作,并且可以在此处查看文档: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-tags 的所有函数列表,所以我想包括我使用的所有函数。
安装 Yoast 插件,您将能够通过手动放置元标记来生成要在搜索引擎结果页面上显示的元标记。
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);
我需要用自定义元描述替换 header.php 中 wp_head() 函数生成的现有 <meta name="description"...>
。页面中的信息不是常规的 wordpress post,是从外部数据库中获取的。
我能够添加我的自定义元数据,但旧的元数据也在那里
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?要么, - 在呈现之前以某种方式替换元描述的值?
Description Meta 标签通常由模板 header (header.php) 或向网站添加描述的插件处理(例如 SEO 标题标签)。由于您得到了重复的描述,您应该检查输出描述标签的插件。
对于放在 header 中的其他烦人的元标记和其他内容,您可以使用模板 functions.php 文件中的 remove_action() 函数来执行此操作,并且可以在此处查看文档: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-tags 的所有函数列表,所以我想包括我使用的所有函数。
安装 Yoast 插件,您将能够通过手动放置元标记来生成要在搜索引擎结果页面上显示的元标记。
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);