这篇文章将给大家举几个关于在框架主题Thematic中使用“filters”和“hooks”的例子,本人觉得很有用,所以就推荐了。
说到WordPress主题框架,Thematic也算是主题爱好者和开发者学习研究的好对象,但是如果你从未接触过诸如“filters”和“hooks”之类的东西,学习起来可能会有些难度。幸好,Thematic主题的官网给我们提供了一份很完整的Thematic 使用指南,如果你正在用的是Thematic主题,那么这正好是你研究学习的好开端,因为既然是以学习研究为主,你应该需要具备一个Thematic子主题,官网那里也给我们提供了好几个不错的子主题,大家可以前往看看,要使用哪一个,各自掂量着吧。
下面所有代码都要放到主题的functions.php文件中去。
1.使用一个Logo替换原来的博客文本标题
//Replace Blog Title with Your Logo
function remove_thematic_blogtitle() {
remove_action('thematic_header','thematic_blogtitle', 3);
}
add_action('init','remove_thematic_blogtitle');
function child_logo_image() {
//Add your own logo image code
//Here's an example
?>
<h1><a href="/"title=""><img src="<?php bloginfo('template_url'); ?>/images/logo.jpg" alt=""/></a></h1>
// End Example
}
add_action('thematic_header','child_logo_image', 3);
2.添加一个搜索框到头部
function add_search(){
include (TEMPLATEPATH . '/searchform.php');
}
add_action('thematic_header','add_search');
3.添加一个 “Home”链接选项到菜单
// Add 'Home' Menu Item
function childtheme_menu_args($args) {
$args = array(
'show_home' => 'Home',
'menu_class' => 'menu',
'echo' => true
);
return $args;
}
add_filter('wp_page_menu_args', 'childtheme_menu_args');
4.去除博客描述
//Remove Blog Description
function remove_thematic_blogdescription() {
remove_action('thematic_header','thematic_blogdescription',5);
}
add_action('init','remove_thematic_blogdescription');
5.去除日志尾部的Footer
add_filter('thematic_postfooter','my_postfooter');
function my_postfooter() {
// The Sound of One Hand Clapping
}
add_filter('thematic_postfooter','my_postfooter');
6.在每篇文章后面添加一个Twitter的链接
function childtheme_postfooter($output) {
$twitterlink = '<p><a href=http://www.twitter.com/chensan899>Follow me on Twitter</a></p>';
return ($twitterlink . $output);
}
add_filter('thematic_postfooter','childtheme_postfooter');
7.添加谷歌统计跟踪代码
//Google Analtyics
function analytic_footer() {?>
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct ="XXXXXXX";
urchinTracker();
</script>
}
add_filter ('thematic_after', 'analytic_footer');
8.添加缩略图支持
// Add Thumbnail Support for Theme (introduced in 2.9)
if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
}
9.将页面底部信息改为“Copyright (Current Year) (Your Blog Name) | (Link to Admin Page) | (Link to RSS)” 的形式
// Generate footer code
function childtheme_footer($thm_footertext) {
$date = date('Y');
$blog_name = get_bloginfo('name');
$admin_url = get_bloginfo('wpurl') . '/wp-admin';
$entries_rss = get_bloginfo('rss2_url');
$thm_footertext = sprintf(
'<p>© %s %s | <a href="http://wptheming.com/2009/10/useful-thematic-filters/">Site Admin</a> | <a href="http://wptheming.com/2009/10/useful-thematic-filters/">Entries RSS</a></p>',
$date, $blog_name, $admin_url, $entries_rss);
return $thm_footertext;
}
add_filter('thematic_footertext', 'childtheme_footer');
10.添加一个Favicon图标
function childtheme_favicon() { ?>
<link rel="shortcut icon" href="<?php echo bloginfo('stylesheet_directory') ?>/images/favicon.ico">
}
add_action('wp_head', 'childtheme_favicon');
11.去除插件代码
许多时候一些常用的插件会存在一些不必要的stylesheets 和 scripts,利用下面的代码可以将它们一 一去除,这是一个例子:
// Remove Page Navi CSS
function childtheme_deregister_styles() {
wp_deregister_style( 'wp-pagenavi' );
}
add_action( 'wp_print_styles', 'childtheme_deregister_styles', 100 );
12.去除或者重命名小工具区块
关于这一部份的介绍大家可以到trunk version of Thematic或者read this post了解详细的教程.
function rename_widgetized_areas($content) {
$content['Primary Aside']['args']['name'] = 'Sidebar';
$content['Index Bottom']['args']['name'] = 'Home Content';
return $content;
}
add_filter('thematic_widgetized_areas', 'rename_widgetized_areas');
13.去除头部的Scripts
如果你不使用下拉式菜单,没有必要加载头部中的scripts代码。
function childtheme_remove_scripts() {
remove_action('wp_head','thematic_head_scripts');
}
add_action('init', 'childtheme_remove_scripts');
原文:wptheming
Pingback 引用通告: 13个有用的Thematic Filters应用技巧 13个有用的Thematic Filters应用技巧 | WordPress加油站
学校里没有学到过,正在自学,博主的文章还是有帮助。