wordpress主题header.php文件为什么要添加wp

html-css013

wordpress主题header.php文件为什么要添加wp,第1张

wp_head函数对于要使用插件的主题来说比较重要,一般如果插件需要向主题头部header标签内加入css文件和js文件,都是通过wp_head()函数输出的。在header.php文件的header标签结束标签</header>前面添加:

<head>

.....

<?php wp_head()?>

</head>

附官方原文解析:

<?php

...

/* Always have wp_head() just before the closing </head>

* tag of your theme, or you will break many plugins, which

* generally use this hook to add elements to <head>such

* as styles, scripts, and meta tags.

*/

wp_head()

?>

</head>

第一步:新建header.php

在做主题文件夹wp-content\themes\goodblog下面新建一个头部文件header.php,然后将index.php中的头部代码提取(剪切)出来,写入这个文件:

第二步:修改index.php

保存好头部文件,在index.php的前面加上代码:

get_header()函数会自动载入主题文件夹中的header.php文件,所以头部文件的文件名命名为header.php然后使用get_deader()函数即可;

现在头部文件已经制作好了,接下来的工作就是编辑头部文件header.php将里面的静态html代码换成动态的php代码。

第三步:修改title

需要更改信息,一般我们将title信息改成这样的: 文章页面标题 | 网站名称,当然用户也可以根据自己的seo只是更改,比如有的人在标题中加入网站描述。将header.php中的<title>index</title>改成下面的代码:

上面的代码通过判断将首页、文章页、分类页、404页面的<title>信息设置成不一样的形式,这样是很有必要的。

几个判断函数的解释如下:

第四步:修改css文件的路径

在header.php文件中找到下面代码:

这个样式表的路径./style.css是网站根目录。

如果用户不想让别人轻易看出用户网站是使用 wordprss程序,那么可以改变图片、样式表等文件的路径,比如本工作室,将图片、样式表都移到了网站根目录,在网页代码中就不会出现 wordprss站才有的wp-content/themes之类的路径。好了,将上面的代码改成:

bloginfo('stylesheet_url')会自动输出网站主题文件夹下style.css文件的绝对网址,如http://localhost/wordPress/wp-content/themes/goodblog/style.css

说到这里,顺便修改一下图片路径,在index.php里有张图片路径需要修改,不过现在可改可不改,后期会自动调用文章图片;

用文本编辑器打开index.php给这些图片加上正确的URL,搜索代码,将所有的:src="images/,批量替换成src="<?php bloginfo('template_url')?>/images/。现在再刷新你的主页,看文章的缩略图是否可以正常显示。

第五步:关于wp_head()

wp_head函数对于要使用插件的主题来说比较重要,一般如果插件需要加载css文件和js文件,都是通过wp_head()函数输出的。在header.php文件的</head>前面添加:

在去看自己的网站,查看网页源代码,会发现<head></head>标签中多了如下代码:

去除wordpress头部不必要的元素标

完整的wordpress头部清理代码

<?php   

//remove_action( ‘wp_head’, ‘wp_enqueue_scripts’, 1 )   

remove_action( ‘wp_head’, ‘feed_links’, 2 )   

remove_action( ‘wp_head’, ‘feed_links_extra’, 3 )   

remove_action( ‘wp_head’, ‘rsd_link’ )   

remove_action( ‘wp_head’, ‘wlwmanifest_link’ )   

remove_action( ‘wp_head’, ‘index_rel_link’ )   

remove_action( ‘wp_head’, ‘parent_post_rel_link’, 10, 0 )   

remove_action( ‘wp_head’, ‘start_post_rel_link’, 10, 0 )   

remove_action( ‘wp_head’, ‘adjacent_posts_rel_link_wp_head’, 10, 0 )   

//remove_action( ‘wp_head’, ‘locale_stylesheet’ )   

remove_action( ‘publish_future_post’, ‘check_and_publish_future_post’, 10, 1 )   

//remove_action( ‘wp_head’, ‘noindex’, 1 )   

//remove_action( ‘wp_head’, ‘wp_print_styles’, 8 )   

//remove_action( ‘wp_head’, ‘wp_print_head_scripts’, 9 )   

remove_action( ‘wp_head’, ‘wp_generator’ )   

//remove_action( ‘wp_head’, ‘rel_canonical’ )   

remove_action( ‘wp_footer’, ‘wp_print_footer_scripts’ )   

remove_action( ‘wp_head’, ‘wp_shortlink_wp_head’, 10, 0 )   

remove_action( ‘template_redirect’, ‘wp_shortlink_header’, 11, 0 )  

 

add_action(‘widgets_init’, ‘my_remove_recent_comments_style’)   

function my_remove_recent_comments_style() {   

global $wp_widget_factory   

remove_action(‘wp_head’, array($wp_widget_factory->widgets['WP_Widget_Recent_Comments'], ‘recent_comments_style’))   

}   

?>

把这段代码插入到主题的functions.php文件下,就可以清除WordPress头部很多的冗余信息。下面说说这些代码的具体意义是什么,以免删除某些你想保留的功能。

wp_head()函数

wp_head()是wordpress的一个非常重要的函数,基本上所有的主题在header.php这个文件里都会使用到这个函数,而且很多插

件为了在header上加点东西也会用到wp_head(),比如SEO的相关插件。不过在wp_head()出现的这个位置,会增加很多并不常用的代

码,如何删除呢?可以通过remove_action移除这些代码。

remove_action函数

函数原型:remove_action( $tag, $function_to_add, $priority, $accepted_args )

该函数移除一个附属于指定动作hook的函数。该方法可用来移除附属于特定动作hook的默认函数,并可能用其它函数取而代之。

重要:添加hook时的$function_to_remove 和$priority参数要能够相匹配,这样才可以移除hook。该原则也适用于过滤器和动作。移除失败时不进行警告提示。 文章来自http://www.life134.com

参数 文章来自http://www.life134.com

1.$tag(字符串)(必需)将要被删除的函数所连接到的动作hook。默认值:None

2.$function_to_remove(回调)(必需) 将要被删除函数的名称默认值:None

3.$priority(整数)(可选)函数优先级(在函数最初连接时定义)默认值:10

4.$accepted_args(整数)(必需)函数所接受参数的数量。默认值:1

返回值

(布尔值)函数是否被移除。

1.Ttue 函数被成功移除

2.False函数未被移除