Wordpress / PHP:如何将元标记放在头顶

问题描述 投票:0回答:3

所以,我有这个PHP函数,它将X-UA-Compatible元标记添加到我的Wordpress网站的每个页面的头部。 (我是Wordpress和PHP的新手,请原谅我的术语)。它位于我的StudioPress子主题的functions.php文件中。

问题是标签只有在非常接近顶部的情况下才有效,而我现在设置它的方式会导致标签出现在中途,所以它不起作用。如何强制此标记出现在每个页面的顶部?

php html wordpress meta
3个回答
4
投票

例如:你可以使用wp_head动作。 wp_head动作钩子由<head></head>函数在用户模板的wp_head()部分中触发。虽然这是主题依赖的,但它是最重要的主题钩子之一,因此得到了广泛的支持。

function aaa_custom_function() {
    ?>
       Your Tag
    <?php
}
add_action('wp_head', 'aaa_custom_function', 1000);

使用第三个参数来控制位置。将1000更改为其他值以找到理想的位置。


1
投票

像这样创建自定义挂钩。

打开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.

0
投票

添加到@Damith回答wp_head行动将起作用。

Refrence

您也可以将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);
© www.soinside.com 2019 - 2024. All rights reserved.