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实例!
head meta标签