wp主题开发中,有时候需要自定义小工具,因为系统的小工具不能满足我们需要,例如我们需要一个热门文章,系统没有,所以只能...,那么废话不哔哔了,直接上代码:

<img src="http://course.51qux.com/wp-content/uploads/2019/01/8d5374c0f6a39a2.jpg" alt="wp主题开发"   />

注册函数:register_sidebar_widget

1.在functions.php添加注册函数

function mb_hot() { include(TEMPLATEPATH . '/hot.php'); }
if( function_exists( 'register_sidebar_widget' ) ) {   
    register_sidebar_widget('热门文章','mb_hot'); 
}

2.新建hot.php模板页面,将代码复制进去

<section class="widget widget_recent_entries">
<h3 class="widget-title">热门文章</h3>
<ul>
<?php query_posts('posts_per_page=6&caller_get_posts=1&orderby=comment_count'); ?>
<?php while (have_posts()) : the_post(); ?>
<li>
<a  href="<?php the_permalink(); ?>" class="text-h1" title="<?php the_title(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile; ?>
</ul>
</section>

这样你的小工具里面就多了一个 “热门文章”的小工具了