wordpress自定义小工具(widget)详解
class My_Widget extends WP_Widget { //继承了 WP_Widget 这个类来创建新的小工具(Widget)
function My_Widget() {
// 主要内容方法
}
function form($instance) {
// 给小工具(widget) 添加表单内容
}
function update($new_instance, $old_instance) {
// 进行更新保存
}
function widget($args, $instance) {
// 输出显示在页面上
}
}
register_widget('My_Widget');
我们可以使用上面的这个代码结构来写一个小例子。代码是这样的:
<?php
class My_Widget extends WP_Widget {
function My_Widget()
{
public function __construct() {
$widget_ops = array( 'classname' => 'dokan-category-menu', 'description' => __( 'Dokan product category menu', 'dokan' ) );
//$this->WP_Widget( 'dokan-category-menu', 'Dokan: Product Category', $widget_ops );
parent::__construct('dokan-category-menu', 'Dokan: Product Category', $widget_ops );
}
}
function form($instance) { // 给小工具(widget) 添加表单内容
$title = esc_attr($instance['title']);
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php esc_attr_e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label></p>
<?php
}
function update($new_instance, $old_instance) { // 更新保存
return $new_instance;
}
function widget($args, $instance) { // 输出显示在页面上
extract( $args );
$title = apply_filters('widget_title', empty($instance['title']) ? __('小测试') : $instance['title']);
?>
<?php echo $before_widget; ?>
<?php if ( $title )
echo $before_title . $title . $after_title; ?>
<?php echo $after_widget; ?>
<?php
}
}
register_widget('My_Widget');
?>
ps: 可以在模板目录下新建一个文件如 widget.php 把上边小工具代码 放入widget.php ,然后把widget.php 文件包含到 functions.php 即可使代码更清晰。包含代码如下:
require_once( dirname(__FILE__) . '/widgets.php' );
<?php
$args = array(
//后台显示的名字
'name' => sprintf(__('Sidebar %d'), $i ),
//边栏的编号
'id' => 'sidebar-1',
//描述
'description' => '',
//小工具的class
'class' => '',
//小工具之前的html代码
'before_widget' => '<li id="%1$s" class="widget %2$s">',
//小工具之后的html代码
'after_widget' => '</li>',
//小工具title之前的html代码
'before_title' => '<h2 class="widgettitle">',
//小工具title之后的html代码
'after_title' => '</h2>' );
//注册小工具
register_sidebar( $args );
?>
<?php
class ultimos_productos_widget extends WP_Widget {
function __construct() {
$widget_ops = array('classname' => 'ultimos_productos_widget',
'description' => "5 ultimos 5 productos por categoría" );
parent::__construct('ultimos_productos_widget', "ultimos 5 productos por categoría", $widget_ops);
}
public function widget($args,$instance){
extract( $args );
$title = apply_filters( 'widget_title', $instance['title']);
$ids = $instance['ids'];
$query_args = array(
'post_status' => 'publish',
'post_type' => 'product',
'posts_per_page' => 5,
'orderby' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $ids,
),
),
);
$r = new WP_Query( $query_args );
if ( $r->have_posts() ) {
echo $before_widget;
if ( $title )
echo $before_title . $title . $after_title;
echo '<ul class="product_list_widget">';
while ( $r->have_posts()) {
$r->the_post();
global $product;
?>
<li>
<a href="<?php echo esc_url( get_permalink( $product->id ) ); ?>" title="<?php echo esc_attr( $product->get_title() ); ?>">
<?php echo $product->get_image(); ?>
<?php echo $product->get_title(); ?>
</a>
<?php if ( ! empty( $show_rating ) ) echo $product->get_rating_html(); ?>
<?php if(is_user_logged_in() ){ ?>
<?php echo $product->get_price_html(); ?>
<?php } ?>
</li>
<?php
}
echo '</ul>';
echo $after_widget;
}
wp_reset_postdata();
echo $content;
}
public function form($instance){
$title = (isset($instance['title'])) ? $instance['title'] : __( 'Products', 'ndb' );
$ids = $instance['ids'];
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title :'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('ids'); ?>"><?php _e('Category :'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('ids'); ?>" name="<?php echo $this->get_field_name( 'ids' ); ?>" type="text" value="<?php echo esc_attr( $ids ); ?>" />
</p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
$instance['ids'] = ( ! empty( $new_instance['ids'] ) ) ? strip_tags( $new_instance['ids'] ) : '';
return $instance;
}
}
function ultimos_productos() {
register_widget( 'ultimos_productos_widget' );
}
add_action( 'widgets_init', 'ultimos_productos' );
写笔记