我已经为查询编写了自定义函数。
<?php
/**
* Column Content (min:2 - max:3 column)
*
* @since 1.0.0
* @return void
*/
if ( ! defined( \'ABSPATH\' ) ) { exit; }
add_action( \'widgets_init\', function(){
register_widget( \'Hw_Column_Widget\' );
});
class Hw_Column_Widget extends WP_Widget {
/**
* Sets up the widgets name etc
*/
public function __construct() {
$widget_ops = array(
\'classname\' => \'hw_column_widget\',
\'description\' => \'Columns widget\',
);
parent::__construct( \'hw_column_widget\', __( \'Home Columns\', \'theme\' ), $widget_ops );
}
/**
* Outputs the content of the widget
*
* @param array $args
* @param array $instance
*/
public function widget( $args, $instance ) {
// outputs the content of the widget
echo $args[\'before_widget\'];
//the_widget($widget, $instance, $args);
if ( ! empty( $instance[\'title_one\'] ) ) {
$t1 = $instance[\'title_one\'];
}
if ( ! empty( $instance[\'title_two\'] ) ) {
$t2 = $instance[\'title_two\'];
}
if ( ! empty( $instance[\'title_three\'] ) ) {
$t3 = $instance[\'title_three\'];
}
$column_first = $instance[\'column_first\'];
$column_second = $instance[\'column_second\'];
$column_third = $instance[\'column_third\'];
echo \'<div class="woocommerce columns-3">\';
switch ($column_first) {
case "recent":
$this->recent($t1);
break;
case "featured":
$this->featured($t1);
break;
case "popular":
$this->popular($t1);
break;
case "onsale":
$this->onsale($t1);
break;
case "best":
$this->best($t1);
break;
}
switch ($column_second) {
case "recent":
$this->recent($t2);
break;
case "featured":
$this->featured($t2);
break;
case "popular":
$this->popular($t2);
break;
case "onsale":
$this->onsale($t2);
break;
case "best":
$this->best($t2);
break;
}
switch ($column_third) {
case "recent":
$this->recent($t3);
break;
case "featured":
$this->featured($t3);
break;
case "popular":
$this->popular($t3);
break;
case "onsale":
$this->onsale($t3);
break;
case "best":
$this->best($t3);
break;
}
echo \'</div>\';
echo $args[\'after_widget\'];
}
/**
* Outputs the options form on admin
*
* @param array $instance The widget options
*/
public function form( $instance ) {
$title= __(\'Home Columns\', \'prostore\');
if(isset($instance[\'title_one\'])) { $title_one = esc_attr($instance[\'title_one\']); }
if(isset($instance[\'title_two\'])) { $title_two = esc_attr($instance[\'title_two\']); }
if(isset($instance[\'title_three\'])) { $title_three = esc_attr($instance[\'title_three\']); }
if(isset($instance[\'column_first\'])) { $column_first = esc_attr($instance[\'column_first\']); }
if(isset($instance[\'column_second\'])) { $column_second = esc_attr($instance[\'column_second\']); }
if(isset($instance[\'column_third\'])) { $column_third = esc_attr($instance[\'column_third\']); }
?>
<h3>1. Kolon</h3>
<p>
<label for="<?php echo $this->get_field_name( \'title_one\' ); ?>"><?php _e( \'Title:\', \'prostore\' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( \'title_one\' ); ?>" name="<?php echo $this->get_field_name( \'title_one\' ); ?>" type="text" value="<?php echo esc_attr( $title_one ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id(\'column_first\'); ?>">İçerik:
<select class=\'widefat\' id="<?php echo $this->get_field_id(\'column_first\'); ?>" name="<?php echo $this->get_field_name(\'column_first\'); ?>">
<option value=\'recent\'<?php echo ($column_first==\'recent\')?\'selected\':\'\'; ?>><?php _e(\'Son Eklenen Ürünler\',\'prostore\'); ?></option>
<option value=\'featured\'<?php echo ($column_first==\'featured\')?\'selected\':\'\'; ?>><?php _e(\'Öne Çıkan Ürünler\',\'prostore\'); ?></option>
<option value=\'popular\'<?php echo ($column_first==\'popular\')?\'selected\':\'\'; ?>><?php _e(\'Popüler Ürünler\',\'prostore\'); ?></option>
<option value=\'onsale\'<?php echo ($column_first==\'onsale\')?\'selected\':\'\'; ?>><?php _e(\'İndirimli Ürünler\',\'prostore\'); ?></option>
<option value=\'best\'<?php echo ($column_first==\'best\')?\'selected\':\'\'; ?>><?php _e(\'Çok Satanlar\',\'prostore\'); ?></option>
</select>
</label>
</p>
<h3>2. Kolon</h3>
<p>
<label for="<?php echo $this->get_field_name( \'title_two\' ); ?>"><?php _e( \'Title:\', \'prostore\' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( \'title_two\' ); ?>" name="<?php echo $this->get_field_name( \'title_two\' ); ?>" type="text" value="<?php echo esc_attr( $title_two ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id(\'column_second\'); ?>">İçerik:
<select class=\'widefat\' id="<?php echo $this->get_field_id(\'column_second\'); ?>" name="<?php echo $this->get_field_name(\'column_second\'); ?>">
<option value=\'recent\'<?php echo ($column_second==\'recent\')?\'selected\':\'\'; ?>><?php _e(\'Son Eklenen Ürünler\',\'prostore\'); ?></option>
<option value=\'featured\'<?php echo ($column_second==\'featured\')?\'selected\':\'\'; ?>><?php _e(\'Öne Çıkan Ürünler\',\'prostore\'); ?></option>
<option value=\'popular\'<?php echo ($column_second==\'popular\')?\'selected\':\'\'; ?>><?php _e(\'Popüler Ürünler\',\'prostore\'); ?></option>
<option value=\'onsale\'<?php echo ($column_second==\'onsale\')?\'selected\':\'\'; ?>><?php _e(\'İndirimli Ürünler\',\'prostore\'); ?></option>
<option value=\'best\'<?php echo ($column_second==\'best\')?\'selected\':\'\'; ?>><?php _e(\'Çok Satanlar\',\'prostore\'); ?></option>
</select>
</label>
</p>
<h3>3. Kolon</h3>
<p>
<label for="<?php echo $this->get_field_name( \'title_three\' ); ?>"><?php _e( \'Title:\', \'prostore\' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( \'title_three\' ); ?>" name="<?php echo $this->get_field_name( \'title_three\' ); ?>" type="text" value="<?php echo esc_attr( $title_three ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id(\'column_third\'); ?>">İçerik:
<select class=\'widefat\' id="<?php echo $this->get_field_id(\'column_third\'); ?>" name="<?php echo $this->get_field_name(\'column_third\'); ?>">
<option value=\'recent\'<?php echo ($column_third==\'recent\')?\'selected\':\'\'; ?>><?php _e(\'Son Eklenen Ürünler\',\'prostore\'); ?></option>
<option value=\'featured\'<?php echo ($column_third==\'featured\')?\'selected\':\'\'; ?>><?php _e(\'Öne Çıkan Ürünler\',\'prostore\'); ?></option>
<option value=\'popular\'<?php echo ($column_third==\'popular\')?\'selected\':\'\'; ?>><?php _e(\'Popüler Ürünler\',\'prostore\'); ?></option>
<option value=\'onsale\'<?php echo ($column_third==\'onsale\')?\'selected\':\'\'; ?>><?php _e(\'İndirimli Ürünler\',\'prostore\'); ?></option>
<option value=\'best\'<?php echo ($column_third==\'best\')?\'selected\':\'\'; ?>><?php _e(\'Çok Satanlar\',\'prostore\'); ?></option>
</select>
</label>
</p>
<?php
}
/**
* Processing widget options on save
*
* @param array $new_instance The new options
* @param array $old_instance The previous options
*/
public function update( $new_instance, $old_instance ) {
// processes widget options to be saved
foreach( $new_instance as $key => $value )
{
$updated_instance[$key] = sanitize_text_field($value);
}
return $updated_instance;
}
/**
* Recent Products
*
*/
public function recent($title = \'\') {
echo \'<div class="widget woocommerce widget_products">\';
echo \'<ul class="product_list_widget">\';
echo \'<h2 class="widgettitle">\'.$title.\'</h2>\';
$args = array(
\'post_type\' => \'product\',
\'status\' => \'publish\',
\'no_found_rows\' => 1,
\'posts_per_page\' => 10,
\'orderby\' => \'date\',
\'order\' => \'DESC\'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product;
echo \'<li>\';
echo \'<a href="\'.esc_url( get_permalink( $product->id ) ).\'" title="\'.esc_attr( $product->get_title() ).\'">\';
echo $product->get_image();
echo \'<span class="product-title">\'.$product->get_title().\'</span>\';
echo \'</a>\';
echo $product->get_price_html();
echo \'</li>\';
endwhile;
wp_reset_query();
echo \'</ul>\';
echo \'</div>\';
}
/**
* Featured Products
*
*/
public function featured($title = \'\') {
echo \'<div class="widget woocommerce widget_products">\';
echo \'<ul class="product_list_widget">\';
echo \'<h2 class="widgettitle">\'.$title.\'</h2>\';
$meta_query = WC()->query->get_meta_query();
$meta_query[] = array(
\'key\' => \'_featured\',
\'value\' => \'yes\'
);
$args = array(
\'post_type\' => \'product\',
\'status\' => \'publish\',
\'no_found_rows\' => 1,
\'posts_per_page\' => 10,
\'orderby\' => \'date\',
\'order\' => \'DESC\',
\'meta_query\' => $meta_query
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product;
echo \'<li>\';
echo \'<a href="\'.esc_url( get_permalink( $product->id ) ).\'" title="\'.esc_attr( $product->get_title() ).\'">\';
echo $product->get_image();
echo \'<span class="product-title">\'.$product->get_title().\'</span>\';
echo \'</a>\';
echo $product->get_price_html();
echo \'</li>\';
endwhile;
wp_reset_query();
echo \'</ul>\';
echo \'</div>\';
}
/**
* Popular Products
*
*/
public function popular($title = \'\') {
echo \'<div class="widget woocommerce widget_products">\';
echo \'<ul class="product_list_widget">\';
echo \'<h2 class="widgettitle">\'.$title.\'</h2>\';
add_filter( \'posts_clauses\', array( WC()->query, \'order_by_rating_post_clauses\' ) );
$args = array(
\'post_type\' => \'product\',
\'status\' => \'publish\',
\'no_found_rows\' => 1,
\'posts_per_page\' => 10,
\'orderby\' => \'date\',
\'order\' => \'DESC\'
);
$args[\'meta_query\'] = WC()->query->get_meta_query();
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product;
echo \'<li>\';
echo \'<a href="\'.esc_url( get_permalink( $product->id ) ).\'" title="\'.esc_attr( $product->get_title() ).\'">\';
echo $product->get_image();
echo \'<span class="product-title">\'.$product->get_title().\'</span>\';
echo \'</a>\';
echo $product->get_price_html();
echo \'</li>\';
endwhile;
remove_filter( \'posts_clauses\', array( WC()->query, \'order_by_rating_post_clauses\' ) );
wp_reset_query();
echo \'</ul>\';
echo \'</div>\';
}
/**
* On Sale Products
*
*/
public function onsale($title = \'\') {
echo \'<div class="widget woocommerce widget_products">\';
echo \'<ul class="product_list_widget">\';
echo \'<h2 class="widgettitle">\'.$title.\'</h2>\';
$args = array(
\'posts_per_page\' => 10,
\'no_found_rows\' => 1,
\'post_status\' => \'publish\',
\'post_type\' => \'product\',
\'meta_query\' => WC()->query->get_meta_query(),
\'post__in\' => array_merge( array( 0 ), wc_get_product_ids_on_sale() )
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product;
echo \'<li>\';
echo \'<a href="\'.esc_url( get_permalink( $product->id ) ).\'" title="\'.esc_attr( $product->get_title() ).\'">\';
echo $product->get_image();
echo \'<span class="product-title">\'.$product->get_title().\'</span>\';
echo \'</a>\';
echo $product->get_price_html();
echo \'</li>\';
endwhile;
wp_reset_query();
echo \'</ul>\';
echo \'</div>\';
}
/**
* Best Selling Products
*
*/
public function best($title = \'\') {
echo \'<div class="widget woocommerce widget_products">\';
echo \'<ul class="product_list_widget">\';
echo \'<h2 class="widgettitle">\'.$title.\'</h2>\';
$args = array(
\'post_type\' => \'product\',
\'status\' => \'publish\',
\'no_found_rows\' => 1,
\'posts_per_page\' => 10,
\'orderby\' => \'meta_value_num\',
\'meta_key\' => \'total_sales\',
\'order\' => \'DESC\'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product;
echo \'<li>\';
echo \'<a href="\'.esc_url( get_permalink( $product->id ) ).\'" title="\'.esc_attr( $product->get_title() ).\'">\';
echo $product->get_image();
echo \'<span class="product-title">\'.$product->get_title().\'</span>\';
echo \'</a>\';
echo $product->get_price_html();
echo \'</li>\';
endwhile;
wp_reset_query();
echo \'</ul>\';
echo \'</div>\';
}
}