非常有用WordPress使用技巧总结1
10、启用文章缩略图功能
从WordPress2.9版本开始,可以给模板添加文章缩略图功能。操作方法很简单,只需要把下面的代码添加到functions.php里面。
1 | add_theme_support( 'post-thumbnails' ); |
然后在要显示缩略图的地方放置下面的代码即可。
1 | <?php the_post_thumbnail(); ?> |
11、自定义WordPress 3.0 版本导航栏
WordPress 3.0 增加了一个功能,可以让WordPress模板开发者自定义导航菜单。如果你想给用户一个导航栏的选择权,只需要把下面的代码加入到 functions.php 文件里面。
1 | add_theme_support( 'nav-menus' ); |
之后把下面的代码复制到你想出新的地方:
1 2 | <?php wp_nav_menu( array( 'sort_column' => 'menu_order', 'container_class' => 'menu-header' ) ); ?> |
12、移除WordPress默认的个人资料选项
如果你客户的想让用户可以自行添加个人资料,那么需要让这个选项更简单。其中一个方法就是移除部分选项,AIM, Yahoo IM 和 Jabber 之类的东东。
1 2 3 4 5 6 7 8 | add_filter('user_contactmethods','hide_profile_fields',10,1); function hide_profile_fields( $contactmethods ) { unset($contactmethods['aim']); unset($contactmethods['jabber']); unset($contactmethods['yim']); return $contactmethods; } |
13、添加作者个人资料选项
如果你想更充分的展示作者的个人资料,那么你可以添加一些更个性化的资料选项,例如添加twitter 和 facebook账号等。下面的代码就是添加twitter 和 facebook账号用的。当然,你可以把里面的内容替换成其他任何你想展示的资料。这个对多博客作者尤其有用。
1 2 3 4 5 6 7 8 9 | function my_new_contactmethods( $contactmethods ) { // Add Twitter $contactmethods['twitter'] = 'Twitter'; //add Facebook $contactmethods['facebook'] = 'Facebook'; return $contactmethods; } add_filter('user_contactmethods','my_new_contactmethods',10,1); |
添加完是上面的代码后,你需要在author.php文件里面添加如下的代码:
1 | <?php echo $curauth->twitter; ?> |
注意:改代码仅在WordPress2.9以上的版本起作用。
14、添加侧边栏小模块
这是目前用的最多的技巧之一,很多WordPress模板开发者都已经知道,并且在用了。
1 2 3 4 5 6 7 8 9 10 11 12 13 | if ( function_exists('register_sidebar') ) register_sidebar(array('name'=>'MiddleSidebar', 'before_widget' => '<li class="widget">', 'after_widget' => '</li>', 'before_title' => '<h2 class="widgettitle">', 'after_title' => '</h3>', )); register_sidebar(array('name'=>'FooterSidebar', 'before_widget' => '<li class="widget">', 'after_widget' => '</li>', 'before_title' => '<h2 class="widgettitle">', 'after_title' => '</h3>', )); |
上面的代码可以增加两个侧边栏的小模块。以此类推,你可以添加无限多侧边栏的小模块。添加完上面的代码后,你需要把下面的代码添加到你要出现这边小模块的地方。
1 2 3 | <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('MiddleSidebar') ) : ?> <!–Default sidebar info goes here–> <?php endif; ?> |
注意:侧边栏并不一定需要出现在sidebar.php文件里面。


欢迎留言