抢注百度搜索高排名流量域名、品牌商标保护、微信绿标安全域名、备案精品短域名,上铭正知产!

 找回密碼
 加入我們

wp_head()函数概述

[複製鏈接]
小猪哼囔 發表於 2023-10-2 22:07:26 | 顯示全部樓層 |閱讀模式

在wp-includes\general-template.php

function wp_head() {
    /**
     * Print scripts or data in the head tag on the front end.
     *
     * @since 1.5.0
     */
    do_action( 'wp_head' );
}

我们经常会在wordpress主题的头部看到wp_head()函数,wp_head()函数是wordpress非常重要的函数,基本上所有的主题在主题头部文件header.php里都会使用到这个函数,同时,许多插件为了在header上加点东西也会用到wp_head()函数,比如网站SEO的相关插件。

但是,在使用wp_head()后,会在网页的头部相应的位置增加很多并不常用的代码,如下面这些代码:

  1. <link rel="alternate" type="application/rss+xml" title="<?php wp_head(); ?>在哪修改?; Feed" href="http://127.0.0.1/feed/" />
  2. <link rel="alternate" type="application/rss+xml" title="<?php wp_head(); ?>在哪修改?; 评论 Feed" href="http://127.0.0.1/comments/feed/" />
  3. <link rel="alternate" type="application/rss+xml" title="<?php wp_head(); ?>在哪修改? 评论 Feed" href="http://127.0.0.1/106.html/feed/" />
  4. <link rel='stylesheet' id='admin-bar-css' href='http://127.0.0.1/wp-includes/css/admin-bar.css?ver=3.4' type='text/css' media='all' />
  5. <link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://127.0.0.1/xmlrpc.php?rsd" />
  6. <link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://127.0.0.1/wp-includes/wlwmanifest.xml" />
  7. <link rel='prev' title='<?php wp_head(); ?>在哪修改?' href='http://127.0.0.1/105.html' />
  8. <link rel='next' title='<?php wp_head(); ?>在哪修改?' href='http://127.0.0.1/107.html' />
  9. <meta name="generator" content="WordPress 3.4" />
  10. <link rel='canonical' href='http://127.0.0.1/106.html' />
  11. <link rel='shortlink' href='http://127.0.0.1/?p=106' />
  12. <style type="text/css" media="print">#wpadminbar { display:none; }</style>
  13. <style type="text/css" media="screen"> html { margin-top: 28px !important; }
  14. * html body { margin-top: 28px !important; }</style>
複製代碼


这些代码中,可能在我们使用的主题当中用不到,就会形成冗余代码。怎么办呢?这时,我们可以通过 remove_action移除这些代码。方法就是将下面的代码加入主题的functions.php文件中。

  1. //去头部信息remove_action( 'wp_head', 'wp_enqueue_scripts', 1 );
  2. remove_action( 'wp_head', 'feed_links', 2 );//文章和评论feed
  3. remove_action( 'wp_head', 'feed_links_extra', 3 );// 额外的feed,例如category, tag页
  4. remove_action( 'wp_head', 'rsd_link' );//ML-RPC
  5. remove_action( 'wp_head', 'wlwmanifest_link' );// 外部编辑器
  6. remove_action( 'wp_head', 'index_rel_link' );//当前文章的索引
  7. remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 );// 父篇
  8. remove_action( 'wp_head', 'start_post_rel_link', 10, 0 );// 开始篇
  9. remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );//rel=pre
  10. remove_action( 'wp_head', 'locale_stylesheet' );
  11. remove_action( 'publish_future_post', 'check_and_publish_future_post', 10, 1 );
  12. remove_action( 'wp_head', 'noindex', 1 );
  13. remove_action( 'wp_head', 'wp_print_styles', 8 );
  14. remove_action( 'wp_head', 'wp_print_head_scripts', 9 );
  15. remove_action( 'wp_head', 'wp_generator' );//移除WordPress版本
  16. //remove_action( 'wp_head', 'rel_canonical' );//移除Canonical标记
  17. remove_action( 'wp_footer', 'wp_print_footer_scripts' );
  18. remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );
  19. remove_action( 'template_redirect', 'wp_shortlink_header', 11, 0 );
  20. add_action('widgets_init', 'my_remove_recent_comments_style');
  21. function my_remove_recent_comments_style() {
  22. global $wp_widget_factory;
  23. remove_action('wp_head', array($wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style'));
  24. }
複製代碼


当然,上面这段代码中有的我们可以需要保留,这时,我们可以在这行代码前加上双斜杠“//”,注释掉这句代码,以免删除掉了我们需要的功能。
回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 加入我們

本版積分規則

备案权重域名预定

QQ|4um創業社區

GMT+8, 2024-5-20 20:07

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回復 返回頂部 返回列表