动态添加 meta 标签_将META标签,脚本和样式表添加到WordPress的页眉和页脚

动态添加 meta 标签

There are times where you may want to conditionally inject stylesheets or scripts into your header or footer, or you'd simply prefer to inject those resources via PHP instead of place the HTML in template files directly.  WordPress' ever-powerful add_filter function provides the a method for injecting stylesheets or scripts into the header, as well as scripts into the footer.  Here's how!

有时您可能希望有条件地将样式表或脚本注入到页眉或页脚中,或者您只希望通过PHP注入这些资源,而不是将HTML直接放置在模板文件中。 WordPress强大的add_filter函数提供了一种将样式表或脚本注入页眉以及将脚本注入页脚的方法。 这是如何做!


// Add items to the header!
function shimify() {
	echo '<!--[if lt IE 9]><script src="/wp-content/themes/mytheme/shim.js"></script><![endif]-->';
	echo '<style>/* some styles here */</style>';
	echo '<meta http-equiv="content-type" content="text/html;" /&gt';
}
add_filter('wp_head', 'shimify');

// Add items to the footer
function add_requirejs() {
	echo '<script src="/wp-content/themes/mytheme/requirejs.js"></script>';
}
add_filter('wp_footer', 'add_requirejs');


The add_filter function, which accepts the filter type, action function, and optional priority, provides the means to inject stylesheets and JavaScript files wherever desired.  You may want to conditionally inject a different Google Analytics account if you use a WordPress install that loads different content depending on hostname.  Do not use this technique if you're adding stylesheets or scripts for your plugin -- those should be added with the proper WordPress functions!

add_filter函数接受过滤器类型,操作函数和可选的优先级,它提供了在需要时注入样式表和JavaScript文件的方法。 如果您使用根据主机名加载不同内容的WordPress安装,则可能需要有条件地注入其他Google Analytics(分析)帐户。 如果您要添加的插件样式表或脚本, 不要使用这种技术-这些应与被添加适当的WordPress的功能

翻译自: https://davidwalsh.name/wp_head

动态添加 meta 标签

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值