在设计WordPress主题时,我发现在functions.php文件里添加一套通用的自定义函数将会大大提高开发效率,因为这样我就可以不必每次开发主题时都需先查找然后复制同样的函数。因此我先搞定functions.php模板然后从那里开始创建主题,模板里把一些必要的准备工作都做好了,包括: 包含 jQuery启用嵌套评论给头部添加Feed链接禁用无用的小工具区域给脚部添加谷歌分析工具 阻止“read more”的跳转 这些函数让我喜欢的共同点就是它们都非常简单明了、高效。此functions.php 模板目前包含了十五个函数,并且还在不断改善中。虽然并不是所有人都会需要使用文件中的所有函数,但我的目的是将这个模板修改为适合大家使用的通用型模板,能够让你通过这些真正实用的函数找到主题开发的突破口。

在规划WordPress主题时,我发现在functions.php文件里增加一套通用的自界说函数将会大大提高开发功率,由于这样我就能够不用每次开发主题时都需先查找然后仿制相同的函数。因而我先搞定functions.php模板然后从那里开端创立主题,模板里把一些必要的准备工作都做好了,包括:

functions.php模板妙用(function php)  functions.php 插件与修改 模板 新闻资讯 第1张

  • 包括 jQuery
  • 启用嵌套谈论
  • 给头部增加Feed链接
  • 禁用无用的小东西区域
  • 给脚部增加谷歌剖析东西
  • 阻挠“read more”的跳转

这些函数让我喜爱的共同点便是它们都十分简单明了、高效。此functions.php 模板现在包括了十五个函数,而且还在不断改进中。尽管并不是一切人都会需求运用文件中的一切函数,但我的意图是将这个模板修正为合适我们运用的通用型模板,能够让你通过这些真实有用的函数找到主题开发的突破口。

在这篇文章里,我先向我们解说下每个函数,然后将一切这十五个函数交融在一起放入到functions.php模板中。你只需仿制并张贴本文最终的代码或是获取 functions.php文件的压缩包 ,就能够通过此模板享用WordPress的基本功能,为您的开发带来的极大的便当。

给头部增加feed链接

WordPress2.8今后,你都能够在头部区域增加一切相关的feed链接(主体、谈论、分类等),不过这并不是默许的,你需求增加下面的代码来运转:

// add feed links to header
if (function_exists('automatic_feed_links')) {
automatic_feed_links();
} else {
return;
}

这段代码先检查你是否运用可兼容的WordPress版别,然后再启用主动geed链接。几点留意事项:榜首,此办法是假定你没有手动在头部增加任何feed链接。第二,依据 最近这个Trac ticket,好像这个功能与add_theme_support现已整合在一起了。

主动包括jQuery

怎样包括 jQuery ? 你能够在主题的functions.php文件里增加下面的代码:

// smart jquery inclusion
if (!is_admin()) {
wp_deregister_script('jquery');
wp_register_script('jquery',

("http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"), false);
wp_enqueue_script('jquery');
}

这个代码能够保证只包括一份jQuery,并从谷歌服务器上拜访它,节约带宽一起拜访时还有缓存上的优势。留意,这段代码必需放在嵌套谈论函数的前面才干正常运转。

启用嵌套谈论

一般来说,启用嵌套谈论需求在头部区域增加一小段代码到wp_head 标签的前面。通过一次小试验后,我发现你能够在functions.php文件里增加这段代码:

// enable threaded comments
function enable_threaded_comments(){
if (!is_admin()) {
if (is_singular() AND comments_open() AND (get_option('thread_comments') == 1))
wp_enqueue_script('comment-reply');
}
}
add_action('get_header', 'enable_threaded_comments');

这有助于坚持 <head> 文件的整齐性,留意,这个函数需求放置在jQuery-inclusion函数的后边才干正常运作。

删去Head区域剩余东西

WordPress <head>文件里含有很多的剩余东西, 比如,版别号、WLW、RSD和索引链接。为了铲除这些不用要信息,你能够在functions.php文件里增加下面的代码:

// remove junk from head
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'feed_links', 2);
remove_action('wp_head', 'index_rel_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'feed_links_extra', 3);
remove_action('wp_head', 'start_post_rel_link', 10, 0);
remove_action('wp_head', 'parent_post_rel_link', 10, 0);
remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0);

给页脚增加谷歌剖析东西

另一个比较让我头疼的工作便是每一次制造站点时都需求在footer.php文件增加谷歌剖析东西的代码。最近我才往functions.php 文件增加了下面的代码并从此不用为这个问题头疼了。

// add google analytics to footer
function add_google_analytics() {
echo '<script src="http://www.google-analytics.com/ga.js" type="text/javascript"></script>';
echo '<script type="text/javascript">';
echo 'var pageTracker = _gat._getTracker("UA-XXXXX-X");';
echo 'pageTracker._trackPageview();';
echo '</script>';
}
add_action('wp_footer', 'add_google_analytics');

两点留意事项:榜首,用你实践的 GA 代码替代“UA-123456-1” ;第二,你也能够检查当时谷歌剖析东西的三种挑选并修正相应的代码。现在,这个函数运用“ga.js”盯梢代码,你也能够改用其他办法。

自界说摘要的长度

运用下面这个函数你就能够给摘要指定任何长度而不用受默许的55字的约束。

// custom excerpt length
function custom_excerpt_length($length) {
return 20;
}
add_filter('excerpt_length', 'custom_excerpt_length');

你只需求将 “20” 替换为任何你需求的字数。

自界说摘要后 “持续阅览字符串

不论你怎样称号这个方括号里的省略号[…]” ,总归这是WordPress默许的紧跟摘要 后边部分,我想删去方括号,运用下面这段代码你能够对它进行任何更改:

// custom excerpt ellipses for 2.9+
function custom_excerpt_more($more) {
return '…';
}
add_filter('excerpt_more', 'custom_excerpt_more');

/* custom excerpt ellipses for 2.8-
function custom_excerpt_more($excerpt) {
return str_replace('[…]', '…', $excerpt);
}
add_filter('wp_trim_excerpt', 'custom_excerpt_more');
*/

你或许现已留意到这儿的代码有两个不同版别,看你运用的是WordPress什么版别。当然最好是运用最新版,因而这儿注释了更老版别的办法,不过要是你需求的话也能够运用它。不论是什么版别,运用此办法你只是需求用 “…” 或是任何你想要的符号来替代本来的“[…]”,即可。

“read more” 链接无法正常跳转

WordPress里最让人难以想象的便是当读者在阅读一篇文章的形式下点击“read more” 链接时,页面就会跳转到 “<!–more–>” 标签的方位。假如是跳转到同一页面也就无所谓了,可是假如是从头加载一个新的页面然后读者发现没有了下文也没有任何解说说哪里出错了,这就很让人觉得不可思议的。无论怎样,这儿有个十分美丽的小函数能够阻挠跳转的产生:

// no more jumping for read more link
function no_more_jumping($post) {
return '<a href="'.get_permalink($post->ID).'" class="read-more">'.'Continue Reading'.'</a>';
}
add_filter('excerpt_more', 'no_more_jumping');

这段代码无需其他任何东西就能够运转,从此你就能够不用为“跳转”操心了。 留意,这也是自界说“read more”链接的好办法,你能够在此给它设定各种特点或界说任何你想要文本。

给博客增加图标

假如你想给博客增加个图标,下面的代码将会十分有用。创立完图标后只需上传图片到网站的根目录下即可。只需在functions.php文件的 <head>区域增加下面的几行代码:

// add a favicon to your
function blog_favicon() {
echo '<link rel="Shortcut Icon" type="image/x-icon" href="'.get_bloginfo('wpurl').'/favicon.ico" />';
}
add_action('wp_head', 'blog_favicon');

你能够随意更改目录,一起保证wp_head包括在你的主题 header.php文件里。

给博客后台增加一个不同的图标

有必要给WordPress后台增加一个特别的图标,这样被保藏为书签或是处理标签时就愈加简单认出。只需将图标上传到主题的/images/ 目录下,加上下面的代码即可:

// add a favicon for your admin
function admin_favicon() {
echo '<link rel="Shortcut Icon" type="image/x-icon"

href="'.get_bloginfo('stylesheet_directory').'/images/favicon.png" />';
}
add_action('admin_head', 'admin_favicon');

像前面相同,你相同能够随意更改目录。不过最好将后台图标和前台图标分隔放在不同的目录下。

自界说后台登陆图标

是否想运用WordPress图标在各个登陆页面给自己做宣扬?那么,你能够将这个WordPress图标替换为其他自界说图片,创立自界说登陆图片,并将其命名为“custom-login-logo.png”将图片上传至主题的/images/ 目录下,用下面的代码:

// custom admin login logo
function custom_login_logo() {
echo '<style type="text/css">
h1 a { background-image:

url('.get_bloginfo('template_directory').'/images/custom-login-logo.png) !important; }
</style>';
}
add_action('login_head', 'custom_login_logo');

这儿关键是要你设置途径和图片称号共同。别的,在创立图片的时分,记住图片的特点:宽为30px, 高为31px,通明GIF格局,头部背景色#464646 。

禁用无用的小东西区域

Justin Tadlock介绍了个十分便利的函数,可用于删去主题中不需求的小东西区域,这是自界说主题必不可少的一个函数:

// disable all widget areas
function disable_all_widgets($sidebars_widgets) {
//if (is_home())
$sidebars_widgets = array(false);
return $sidebars_widgets;
}
add_filter('sidebars_widgets', 'disable_all_widgets');

这个代码归于即插即用型,不需求任何更改。留意:假如你只想在主页禁用小东西,那么就将第三栏的 “//”删去。

删去WordPress更新提示

我比较厌烦WordPress更新提示,下面的代码就能够将办理面板的更新提示删去:

// kill the admin nag
if (!current_user_can('edit_users')) {
add_action('init', create_function('$a', "remove_action('init', 'wp_version_check');"), 2);
add_filter('pre_option_update_core', create_function('$a', "return null;"));
}

假如你想要取得更新告诉的话,你也能够将这段代码注释掉或是删去掉。

body_class 与 post_class中参加分类ID

默许情况下,WordPress body_class和 post_class并没有包括当时文章的分类ID。 不过,你能够用下面的代码来完成:

// category id in body and post class
function category_id_class($classes) {
global $post;
foreach((get_the_category($post->ID)) as $category)
$classes [] = 'cat-' . $category->cat_ID . '-id';
return $classes;
}
add_filter('post_class', 'category_id_class');
add_filter('body_class', 'category_id_class');

即便你没有运用分类ID,可是这个函数仍是十分好使的,这也是为什么我将这个归入自界说functions.php 模板的必备函数中来。

获取榜首个分类ID

当要处理多个不同分类时,另一个十分有用的函数能够让你获取当时文章的榜首个分类。代码如下:

// get the first category id
function get_first_category_ID() {
$category = get_the_category();
return $category[0]->cat_ID;
}

严厉的即插即播型: 只需在模板文件运用<?php get_first_category_ID(); ?>来拜访数据。

整合以上一切代码

如最初许诺,这儿将完好的代码贴出来与我们共享:

<?php // custom functions.php template @ digwp.com

// add feed links to header
if (function_exists('automatic_feed_links')) {
automatic_feed_links();
} else {
return;
}

// smart jquery inclusion
if (!is_admin()) {
wp_deregister_script('jquery');
wp_register_script('jquery',

("http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"), false, '1.3.2');
wp_enqueue_script('jquery');
}

// enable threaded comments
function enable_threaded_comments(){
if (!is_admin()) {
if (is_singular() AND comments_open() AND (get_option('thread_comments') == 1))
wp_enqueue_script('comment-reply');
}
}
add_action('get_header', 'enable_threaded_comments');

// remove junk from head
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'feed_links', 2);
remove_action('wp_head', 'index_rel_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'feed_links_extra', 3);
remove_action('wp_head', 'start_post_rel_link', 10, 0);
remove_action('wp_head', 'parent_post_rel_link', 10, 0);
remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0);

// add google analytics to footer
function add_google_analytics() {
echo '<script

src="http://www.google-analytics.com/ga.js" type="text/javascript"></script>';
echo '<script type="text/javascript">';
echo 'var pageTracker = _gat._getTracker("UA-XXXXX-X");';
echo 'pageTracker._trackPageview();';
echo '</script>';
}
add_action('wp_footer', 'add_google_analytics');

// custom excerpt length
function custom_excerpt_length($length) {
return 20;
}
add_filter('excerpt_length', 'custom_excerpt_length');

// custom excerpt ellipses for 2.9+
function custom_excerpt_more($more) {
return '…';
}
add_filter('excerpt_more', 'custom_excerpt_more');

/* custom excerpt ellipses for 2.8-
function custom_excerpt_more($excerpt) {
return str_replace('[…]', '…', $excerpt);
}
add_filter('wp_trim_excerpt', 'custom_excerpt_more');
*/

// no more jumping for read more link
function no_more_jumping($post) {
return '<a href="'.get_permalink($post->ID).'

" class="read-more">'.'Continue Reading'.'</a>';
}
add_filter('excerpt_more', 'no_more_jumping');

// add a favicon to your
function blog_favicon() {
echo '<link rel="Shortcut Icon" type="image/x-icon"

href="'.get_bloginfo('wpurl').'/favicon.ico" />';
}
add_action('wp_head', 'blog_favicon');

// add a favicon for your admin
function admin_favicon() {
echo '<link rel="Shortcut Icon" type="image/x-icon"

href="'.get_bloginfo('stylesheet_directory').'/images/favicon.png" />';
}
add_action('admin_head', 'admin_favicon');

// custom admin login logo
function custom_login_logo() {
echo '<style type="text/css">
h1 a { background-image:

url('.get_bloginfo('template_directory').'/images/custom-login-logo.png) !important; }
</style>';
}
add_action('login_head', 'custom_login_logo');

// disable all widget areas
function disable_all_widgets($sidebars_widgets) {
//if (is_home())
$sidebars_widgets = array(false);
return $sidebars_widgets;
}
add_filter('sidebars_widgets', 'disable_all_widgets');

// kill the admin nag
if (!current_user_can('edit_users')) {
add_action('init', create_function('$a',

"remove_action('init', 'wp_version_check');"), 2);
add_filter('pre_option_update_core', create_function('$a', "return null;"));
}

// category id in body and post class
function category_id_class($classes) {
global $post;
foreach((get_the_category($post->ID)) as $category)
$classes [] = 'cat-' . $category->cat_ID . '-id';
return $classes;
}
add_filter('post_class', 'category_id_class');
add_filter('body_class', 'category_id_class');

// get the first category id
function get_first_category_ID() {
$category = get_the_category();
return $category[0]->cat_ID;
}

?>

下载自界说functions.php 模板文件

假如喜爱的话,你能够下载functions.php 模板文件压缩包

原文:WordPress functions.php Template with 15 Essential CustomFunctions

转载请说明出处
知优网 » functions.php模板妙用(function php)

发表评论

您需要后才能发表评论