所以,我有这个PHP函数,它将X-UA-Compatible元标记添加到我的Wordpress网站的每个页面的头部。 (我是Wordpress和PHP的新手,请原谅我的术语)。它位于我的StudioPress子主题的functions.php文件中。
问题是标签只有在非常接近顶部的情况下才有效,而我现在设置它的方式会导致标签出现在中途,所以它不起作用。如何强制此标记出现在每个页面的顶部?
例如:你可以使用wp_head
动作。 wp_head动作钩子由<head></head>
函数在用户模板的wp_head()
部分中触发。虽然这是主题依赖的,但它是最重要的主题钩子之一,因此得到了广泛的支持。
function aaa_custom_function() {
?>
Your Tag
<?php
}
add_action('wp_head', 'aaa_custom_function', 1000);
使用第三个参数来控制位置。将1000更改为其他值以找到理想的位置。
像这样创建自定义挂钩。
打开header.php然后在下面的行中写下要显示元标记的行
<?php do_action("my_custom_metatags");?>
现在打开你的functions.php并记下下面的代码
function my_custom_metatags_fn()
{
//your tag goes here.
}
add_action("my_custom_metatags","my_custom_metatags_fn");
//Test this code it will work like a charm for you.
添加到@Damith回答wp_head
行动将起作用。
您也可以将header.php
文件覆盖到您的子主题中。
在function.php
中添加此代码
<?php function your_custom_function() {
// your custom code which you want to add in header file
}
add_action('wp_head', 'your_custom_function', 1);