head meta标签_将META标签添加到WordPress Head

head meta标签

The reason that WordPress is so developer-friendly is their awesome hook system.  Depending on a post category (or seemingly anything else), you can modify the contents of the page, the theme templates, and everything else.  WordPress is amazing.

WordPress之所以对开发人员如此友好的原因是其出色的挂钩系统。 根据帖子类别(或看似其他内容),您可以修改页面,主题模板以及其他所有内容。 WordPress很棒。

One recent use case I had was wanting to modify the <meta name="robots" content="{index? follow?}"> tag based on the post's category:  if the post is in category A, use noindex,nofollow, otherwise use index,follow.  After all not every post is a reflection of the overall site content.

我最近的一个用例是要根据帖子的类别修改<meta name="robots" content="{index? follow?}">标签:如果帖子属于A类,请使用noindex,nofollow ,否则使用index,follow 。 毕竟,并非每个帖子都反映了整个网站的内容。

Completing the task was easy using the WordPress hook system, specifically the wp_head hook:

使用WordPress挂钩系统,特别是wp_head挂钩,可以轻松完成任务:


// Worker function - echo tags to be added to the header
function dont_follow_some_posts() {
  global $IMPORTANT_CATEGORIES, $post;

  if(isset($post) && is_single($post) && in_category($IMPORTANT_CATEGORIES['sponsored'], $post->id)) {
    echo '
   
   ';
  }
  else {
    echo '
   
   ';
  }
}

// Hook into "wp_head" to add meta tags
add_filter('wp_head', 'dont_follow_some_posts');


You can use this hook to add anything to the site <head> but CSS and JavaScript files have their own methods so don't use this hook to add styles and scripts.

您可以使用此挂钩向网站<head>添加任何内容 ,但是CSS和JavaScript文件具有自己的方法,因此请勿使用此挂钩添加样式和脚本。

Take control of your WordPress instances by using the awesome hook system!

使用超赞的钩子系统控制您的WordPress实例!

翻译自: https://davidwalsh.name/wordpress-add-meta-tags

head meta标签

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值