Thanks for reply I am very grateful Kārlis Millers, dingo-d now my widget it looks how.
<?php
class ultimos_productos_widget_1 extends WP_Widget {
function __construct() {
parent::__construct(
'ultimos_productos_widget', __( 'ultimos_productos_widget_1', 'tutsplustextdomain' ),
array(
'classname' => 'ultimos_productos_widget_1',
'description' => __( 'A basic text widget to demo the Tutsplus series on creating your own widgets.', 'tutsplustextdomain' )
)
);
}
public function widget($args,$instance){
extract( $args );
echo $before_widget = '<div class="sidebar woocommerce">' ;
?>
<h3 class="widget-title"> <?php _e('Ultimate Products', 'pruebadesarrollo'); ?></h3>
<ul class="products">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 5,
'product_cat' => 'marble',
'orderby' => 'ASC',
/*'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'marble',
),
),*/
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<li class="product">
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php woocommerce_show_product_sale_flash( $post, $product ); ?>
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>
<h3><?php the_title(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
<?php if(is_user_logged_in() ){ ?>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
<?php } ?>
</li>
<?php endwhile; ?>
<?php wp_reset_postdata() ?>
</ul><!--/.products-->
<?php
echo $after_widget='</div>';
}
public function update($new_instance, $old_instance){
// Función de guardado de opciones
}
public function form($instance){
// Formulario de opciones del Widget, que aparece cuando añadimos el Widget a una Sidebar
}
}
function myplugin_register_widgets() {
register_widget( 'ultimos_productos_widget_1' );
}
add_action( 'widgets_init', 'myplugin_register_widgets' );
?>
It was working before of update a to wordpress 4.3.
In wp_debug show me this message:
Notice: The called constructor method for WP_Widget is deprecated since version 4.3.0! Use
Internet research I found that I should change the next line.
$this->WP_Widget('ultimos_productos_widget', "ultimos 5 proyectos", $widget_ops);
for
parent::__construct('ultimos_productos_widget', "ultimos 5 proyectos", $widget_ops);
I dont know what happened?
Widget need register after you created it
Just put after your class:
register_widget('ultimos_productosaluminum_widget');
Or in wordpress style:
function register_ultimos_widget() {
register_widget('ultimos_productosaluminum_widget');
}
add_action( 'widgets_init', 'register_ultimos_widget' );
See more at CODEX
Thanks for your comments,
I modified the code with the suggestions y this is my custom widget.
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' );
This a plugin and works perfectly.
Thanks.
Related
I need to show users order list with my own html and css, like the number and the thumbnail in the left part of the item, and in the right part it has to be an order date, an order status, the quantity and the price, and my own meta.
I've founded how to add an image with the hook, it's not a problem. The problem is - that I can't customize the native structure of woocommerce/myaccount/orders.php. Even when I change table, tr,td,tbody to div-s, my wrappers don't help. It's a loop. And all that I add is adding for all items
foreach ( $customer_orders->orders as $customer_order ) {
$order = wc_get_order( $customer_order ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$item_count = $order->get_item_count() - $order->get_item_count_refunded();
?>
<div class="woocommerce-orders-table__row woocommerce-orders-table__row--status-<?php echo esc_atdiv( $order->get_status() ); ?> order">
<?php foreach ( wc_get_account_orders_columns() as $column_id => $column_name ) : ?>
<div class="woocommerce-orders-table__cell woocommerce-orders-table__cell-<?php echo esc_atdiv( $column_id ); ?>" data-title="<?php echo esc_atdiv( $column_name ); ?>">
<?php if ( has_action( 'woocommerce_my_account_my_orders_column_' . $column_id ) ) : ?>
<?php do_action( 'woocommerce_my_account_my_orders_column_' . $column_id, $order ); ?>
<?php elseif ( 'order-number' === $column_id ) : ?>
<a href="<?php echo esc_url( $order->get_view_order_url() ); ?>">
<?php echo esc_html( _x( '#', 'hash before order number', 'woocommerce' ) . $order->get_order_number() ); ?>
</a>
<?php elseif ( 'order-date' === $column_id ) : ?>
<time datetime="<?php echo esc_atdiv( $order->get_date_created()->date( 'c' ) ); ?>"><?php echo esc_html( wc_format_datetime( $order->get_date_created() ) ); ?></time>
<?php elseif ( 'order-status' === $column_id ) : ?>
<?php echo esc_html( wc_get_order_status_name( $order->get_status() ) ); ?>
<?php elseif ( 'order-total' === $column_id ) : ?>
<?php
/* divanslators: 1: formatted order total 2: total order items */
echo wp_kses_post( sprintf( _n( '%1$s for %2$s item', '%1$s for %2$s items', $item_count, 'woocommerce' ), $order->get_formatted_order_total(), $item_count ) );
?>
<?php elseif ( 'order-actions' === $column_id ) : ?>
<?php
$actions = wc_get_account_orders_actions( $order );
if ( ! empty( $actions ) ) {
foreach ( $actions as $key => $action ) { // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
echo '' . esc_html( $action['name'] ) . '';
}
}
?>
<?php endif; ?>
</div>
<?php endforeach;
I need to separate the order-number and the added thumbnail to one wrapper, and all other metas to another.
I need something like global $product; product-> get_price(); but with the order.
Maybe there is some hooks of functions to display all order parts separately, and make a new loop?
As per our understanding, you want to create your own template page and show current user orders.
If I am right you should try like this:
<?php
/*
* Template Name: Order Page Template
*/
defined( 'ABSPATH' ) || exit;
global $woocommerce, $user_id;
if (!class_exists('WooCommerce') || !get_current_user_id()) {
return;
};
$user_id = get_current_user_id();
//$customer = wp_get_current_user();
$posts_per_page = 20;
// Get all customer orders
$customer__all_orders = get_posts(apply_filters('woocommerce_my_account_my_orders_query', array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'orderby' => 'date',
'order' => 'DESC',
'meta_value' => $user_id ,
'post_type' => wc_get_order_types(),
'post_status' => array_keys(wc_get_order_statuses()), 'post_status' => array('wc-processing'),
)));
$paged = isset($_REQUEST['order_page']) ? $_REQUEST['order_page'] : 1;
$total_records = count($customer__all_orders);
$total_pages = ceil($total_records / $posts_per_page);
$customer_orders = get_posts(array(
'meta_key' => '_customer_user',
'order' => 'DESC',
'meta_value' => $user_id ,
'post_type' => wc_get_order_types(),
'posts_per_page' => $posts_per_page,
'paged' => $paged,
'post_status' => array_keys(wc_get_order_statuses()), 'post_status' => array('wc-processing'),
));
?>
<?php if (!empty($customer_orders)) : ?>
<table class="woocommerce-orders-table woocommerce-MyAccount-orders shop_table shop_table_responsive my_account_orders account-orders-table">
<thead>
<tr>
<?php foreach ( wc_get_account_orders_columns() as $column_id => $column_name ) : ?>
<th class="woocommerce-orders-table__header woocommerce-orders-table__header-<?php echo esc_attr( $column_id ); ?>"><span class="nobr"><?php echo esc_html( $column_name ); ?></span></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order( $customer_order );
$item_count = $order->get_item_count() - $order->get_item_count_refunded();
?>
<tr class="woocommerce-orders-table__row woocommerce-orders-table__row--status-<?php echo esc_attr( $order->get_status() ); ?> order">
<?php foreach ( wc_get_account_orders_columns() as $column_id => $column_name ) : ?>
<td class="woocommerce-orders-table__cell woocommerce-orders-table__cell-<?php echo esc_attr( $column_id ); ?>" data-title="<?php echo esc_attr( $column_name ); ?>">
<?php if ( has_action( 'woocommerce_my_account_my_orders_column_' . $column_id ) ) : ?>
<?php do_action( 'woocommerce_my_account_my_orders_column_' . $column_id, $order ); ?>
<?php elseif ( 'order-number' === $column_id ) : ?>
<a href="<?php echo esc_url( $order->get_view_order_url() ); ?>">
<?php echo esc_html( _x( '#', 'hash before order number', 'woocommerce' ) . $order->get_order_number() ); ?>
</a>
<?php elseif ( 'order-date' === $column_id ) : ?>
<time datetime="<?php echo esc_attr( $order->get_date_created()->date( 'c' ) ); ?>"><?php echo esc_html( wc_format_datetime( $order->get_date_created() ) ); ?></time>
<?php elseif ( 'order-status' === $column_id ) : ?>
<?php echo esc_html( wc_get_order_status_name( $order->get_status() ) ); ?>
<?php elseif ( 'order-total' === $column_id ) : ?>
<?php
/* translators: 1: formatted order total 2: total order items */
echo wp_kses_post( sprintf( _n( '%1$s for %2$s item', '%1$s for %2$s items', $item_count, 'woocommerce' ), $order->get_formatted_order_total(), $item_count ) );
?>
<?php elseif ( 'order-actions' === $column_id ) : ?>
<?php
$actions = wc_get_account_orders_actions( $order );
if ( ! empty( $actions ) ) {
foreach ( $actions as $key => $action ) { // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
echo '' . esc_html( $action['name'] ) . '';
}
}
?>
<?php endif; ?>
</td>
<?php endforeach; ?>
</tr>
<?php
}
?>
</tbody>
</table>
<div class="pagination">
<?php
$args = array(
'base' => '%_%',
'format' => '?order_page=%#%',
'total' => $total_pages,
'current' => $paged,
'show_all' => False,
'end_size' => 5,
'mid_size' => 5,
'prev_next' => True,
'prev_text' => __('« Previous'),
'next_text' => __('Next »'),
'type' => 'plain',
'add_args' => False,
'add_fragment' => ''
);
echo paginate_links($args);
?>
</div>
<?php else : ?>
<div class="woocommerce-message woocommerce-message--info woocommerce-Message woocommerce-Message--info woocommerce-info">
<a class="woocommerce-Button button" href="<?php echo esc_url( apply_filters( 'woocommerce_return_to_shop_redirect', wc_get_page_permalink( 'shop' ) ) ); ?>"><?php esc_html_e( 'Browse products', 'woocommerce' ); ?></a>
<?php esc_html_e( 'No order has been made yet.', 'woocommerce' ); ?>
</div>
<?php endif; ?>
Steps:
Create new page inside your active theme - template-order.php
Copy past above code inside your template-order.php apge and save.
Goto wordpress admin dashboard pages menu and click on new page
After create new page assign templete "Order Page Templete" and save
Now open your new page and you will get or order related to current logged in user
Note: You can also modify above code accordingly
hi i need to registry two widget of two post type. the first type is movies and the second is news
i have add the widget to function but is showing just one widget of them.
i have create two classes and when i test only one of theme its working good ? but when i add it together i see only one of them
this is my code
class Movies_Recent_Posts extends WP_Widget {
public function __construct() {
$widget_ops = array('classname' => 'Movies_Recent_Posts', 'description' => esc_html__( "Latest movies.",'artor') );
parent::__construct('wpsites-recent-posts', esc_html__('Recent Movies','artor'), $widget_ops);
$this->alt_option_name = 'Movies_Recent_Posts';
add_action( 'save_post', array($this, 'flush_widget_cache') );
add_action( 'deleted_post', array($this, 'flush_widget_cache') );
add_action( 'switch_theme', array($this, 'flush_widget_cache') );
}
public function widget($args, $instance) {
$cache = array();
if ( ! $this->is_preview() ) {
$cache = wp_cache_get( 'movies_widget_recent_posts', 'widget' );
}
if ( ! is_array( $cache ) ) {
$cache = array();
}
if ( ! isset( $args['widget_id'] ) ) {
$args['widget_id'] = $this->id;
}
if ( isset( $cache[ $args['widget_id'] ] ) ) {
echo $cache[ $args['widget_id'] ];
return;
}
ob_start();
$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : esc_html__( 'Recent Movies','artor' );
/** This filter is documented in wp-includes/default-widgets.php */
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
$number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
if ( ! $number )
$number = 5;
$show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
$r = new WP_Query( apply_filters( 'widget_posts_args', array(
'posts_per_page' => $number,
'no_found_rows' => true,
'post_status' => 'publish',
'post_type' => array('movies',
'ignore_sticky_posts' => true
) ) ) );
if ($r->have_posts()) :
?>
<?php echo $args['before_widget']; ?>
<?php if ( $title ) {
echo $args['before_title'] . $title . $args['after_title'];
} ?>
<ul>
<?php while ( $r->have_posts() ) : $r->the_post(); ?>
<li>
<?php get_the_title() ? the_title() : the_ID(); ?>
<?php if ( $show_date ) : ?>
<small class="wmovie-date"><?php echo get_the_date(); ?></small>
<hr>
<?php endif; ?>
</li>
<?php endwhile; ?>
</ul>
<?php echo $args['after_widget']; ?>
<?php
wp_reset_postdata();
endif;
if ( ! $this->is_preview() ) {
$cache[ $args['widget_id'] ] = ob_get_flush();
wp_cache_set( 'movies_widget_recent_posts', $cache, 'widget' );
} else {
ob_end_flush();
}
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['number'] = (int) $new_instance['number'];
$instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
$this->flush_widget_cache();
$alloptions = wp_cache_get( 'alloptions', 'options' );
if ( isset($alloptions['Movies_Recent_Posts']) )
delete_option('Movies_Recent_Posts');
return $instance;
}
public function flush_widget_cache() {
wp_cache_delete('movies_widget_recent_posts', 'widget');
}
public function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
$number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
$show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
?>
<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php echo esc_html__( 'Title:' ,'artor'); ?></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 $title; ?>" /></p>
<p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php echo esc_html__( 'Number of movies to show:','artor' ); ?></label>
<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
<p><input class="checkbox" type="checkbox" <?php checked( $show_date ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" />
<label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php echo esc_html__( 'Display movies date?','artor' ); ?></label></p>
<?php
}
}
class News_Recent_Posts extends WP_Widget {
public function __construct() {
$widget_news = array('classname' => 'News_Recent_Posts', 'description' => esc_html__( 'Recent News.','artor') );
parent::__construct('wpsites-recent-posts', esc_html__('Recent News','artor'), $widget_news);
$this->alt_option_name = 'News_Recent_Posts';
add_action( 'save_post', array($this, 'flush_widget_cache') );
add_action( 'deleted_post', array($this, 'flush_widget_cache') );
add_action( 'switch_theme', array($this, 'flush_widget_cache') );
}
public function widget($args, $instance_news) {
$cache_news = array();
if ( ! $this->is_preview() ) {
$cache_news = wp_cache_get( 'news_widget_recent_posts', 'widget' );
}
if ( ! is_array( $cache_news ) ) {
$cache_news = array();
}
if ( ! isset( $args['widget_id'] ) ) {
$args['widget_id'] = $this->id;
}
if ( isset( $cache_news[ $args['widget_id'] ] ) ) {
echo $cache_news[ $args['widget_id'] ];
return;
}
ob_start();
$title_news = ( ! empty( $instance_news['title'] ) ) ? $instance_news['title'] : esc_html__( 'Recent news','artor' );
/** This filter is documented in wp-includes/default-widgets.php */
$title_news = apply_filters( 'widget_title', $title_news, $instance_news, $this->id_base );
$number_news = ( ! empty( $instance_news['number'] ) ) ? absint( $instance_news['number'] ) : 5;
if ( ! $number_news )
$number_news = 5;
$show_date_news = isset( $instance_news['show_date'] ) ? $instance_news['show_date'] : false;
$r_news = new WP_Query( apply_filters( 'widget_posts_args', array(
'posts_per_page' => $number_news,
'no_found_rows' => true,
'post_status' => 'publish',
'post_type' => array('news',
'ignore_sticky_posts' => true
) ) ) );
if ($r_news->have_posts()) :
?>
<?php echo $args['before_widget']; ?>
<?php if ( $title_news ) {
echo $args['before_title'] . $title_news . $args['after_title'];
} ?>
<ul>
<?php while ( $r_news->have_posts() ) : $r_news->the_post(); ?>
<li>
<?php get_the_title() ? the_title() : the_ID(); ?>
<?php if ( $show_date_news ) : ?>
<span class="post-date"><?php echo get_the_date(); ?></span>
<?php endif; ?>
</li>
<?php endwhile; ?>
</ul>
<?php echo $args['after_widget']; ?>
<?php
wp_reset_postdata();
endif;
if ( ! $this->is_preview() ) {
$cache_news[ $args['widget_id'] ] = ob_get_flush();
wp_cache_set( 'news_widget_recent_posts', $cache_news, 'widget' );
} else {
ob_end_flush();
}
}
public function update( $new_instance, $old_instance_news ) {
$instance_news = $old_instance_news;
$instance_news['title'] = strip_tags($new_instance['title']);
$instance_news['number'] = (int) $new_instance['number'];
$instance_news['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
$this->flush_widget_cache();
$alloptions_news = wp_cache_get( 'alloptions', 'options' );
if ( isset($alloptions_news['News_Recent_Posts']) )
delete_option('News_Recent_Posts');
return $instance_news;
}
public function flush_widget_cache() {
wp_cache_delete('news_widget_recent_posts', 'widget');
}
public function form( $instance_news ) {
$title_news = isset( $instance_news['title'] ) ? esc_attr( $instance_news['title'] ) : '';
$number_news = isset( $instance_news['number'] ) ? absint( $instance_news['number'] ) : 5;
$show_date_news = isset( $instance_news['show_date'] ) ? (bool) $instance_news['show_date'] : false;
?>
<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php echo esc_html__( 'Title:','artor' ); ?></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 $title_news; ?>" /></p>
<p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php echo esc_html__( 'Number of news to show:','artor' ); ?></label>
<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number_news; ?>" size="3" /></p>
<p><input class="checkbox" type="checkbox" <?php checked( $show_date_news ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" />
<label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php echo esc_html__( 'Display news date?' ,'artor'); ?></label></p>
<?php
}
}
function load_widgets() {
register_widget('Movies_Recent_Posts');
register_widget('News_Recent_Posts');
}
add_action( 'widgets_init', 'load_widgets');
Sorry i have fixed . thank you all
class Movies_Recent_Posts extends WP_Widget {
public function __construct() {
$widget_ops = array('classname' => 'Movies_Recent_Posts', 'description' => esc_html__( "Latest movies.",'artor') );
parent::__construct('widjet-recent-movies', esc_html__('Recent Movies','artor'), $widget_ops);
$this->alt_option_name = 'Movies_Recent_Posts';
add_action( 'save_post', array($this, 'flush_widget_cache') );
add_action( 'deleted_post', array($this, 'flush_widget_cache') );
add_action( 'switch_theme', array($this, 'flush_widget_cache') );
}
public function widget($args, $instance) {
$cache = array();
if ( ! $this->is_preview() ) {
$cache = wp_cache_get( 'movies_widget_recent_posts', 'widget' );
}
if ( ! is_array( $cache ) ) {
$cache = array();
}
if ( ! isset( $args['widget_id'] ) ) {
$args['widget_id'] = $this->id;
}
if ( isset( $cache[ $args['widget_id'] ] ) ) {
echo $cache[ $args['widget_id'] ];
return;
}
ob_start();
$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : esc_html__( 'Recent Movies','artor' );
/** This filter is documented in wp-includes/default-widgets.php */
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
$number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
if ( ! $number )
$number = 5;
$show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
$r = new WP_Query( apply_filters( 'widget_posts_args', array(
'posts_per_page' => $number,
'no_found_rows' => true,
'post_status' => 'publish',
'post_type' => array('movies',
'ignore_sticky_posts' => true
) ) ) );
if ($r->have_posts()) :
?>
<?php echo $args['before_widget']; ?>
<?php if ( $title ) {
echo $args['before_title'] . $title . $args['after_title'];
} ?>
<ul>
<?php while ( $r->have_posts() ) : $r->the_post(); ?>
<li>
<?php get_the_title() ? the_title() : the_ID(); ?>
<?php if ( $show_date ) : ?>
<small class="wmovie-date"><?php echo get_the_date(); ?></small>
<hr>
<?php endif; ?>
</li>
<?php endwhile; ?>
</ul>
<?php echo $args['after_widget']; ?>
<?php
wp_reset_postdata();
endif;
if ( ! $this->is_preview() ) {
$cache[ $args['widget_id'] ] = ob_get_flush();
wp_cache_set( 'movies_widget_recent_posts', $cache, 'widget' );
} else {
ob_end_flush();
}
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['number'] = (int) $new_instance['number'];
$instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
$this->flush_widget_cache();
$alloptions = wp_cache_get( 'alloptions', 'options' );
if ( isset($alloptions['Movies_Recent_Posts']) )
delete_option('Movies_Recent_Posts');
return $instance;
}
public function flush_widget_cache() {
wp_cache_delete('movies_widget_recent_posts', 'widget');
}
public function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
$number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
$show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
?>
<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php echo esc_html__( 'Title:' ,'artor'); ?></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 $title; ?>" /></p>
<p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php echo esc_html__( 'Number of movies to show:','artor' ); ?></label>
<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
<p><input class="checkbox" type="checkbox" <?php checked( $show_date ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" />
<label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php echo esc_html__( 'Display movies date?','artor' ); ?></label></p>
<?php
}
}
class News_Recent_Posts extends WP_Widget {
public function __construct() {
$widget_news = array('classname' => 'News_Recent_Posts', 'description' => esc_html__( 'Recent News.','artor') );
parent::__construct('widjet-recent-news', esc_html__('Recent News','artor'), $widget_news);
$this->alt_option_name = 'News_Recent_Posts';
add_action( 'save_post', array($this, 'flush_widget_cache') );
add_action( 'deleted_post', array($this, 'flush_widget_cache') );
add_action( 'switch_theme', array($this, 'flush_widget_cache') );
}
public function widget($args, $instance_news) {
$cache_news = array();
if ( ! $this->is_preview() ) {
$cache_news = wp_cache_get( 'news_widget_recent_posts', 'widget' );
}
if ( ! is_array( $cache_news ) ) {
$cache_news = array();
}
if ( ! isset( $args['widget_id'] ) ) {
$args['widget_id'] = $this->id;
}
if ( isset( $cache_news[ $args['widget_id'] ] ) ) {
echo $cache_news[ $args['widget_id'] ];
return;
}
ob_start();
$title_news = ( ! empty( $instance_news['title'] ) ) ? $instance_news['title'] : esc_html__( 'Recent news','artor' );
/** This filter is documented in wp-includes/default-widgets.php */
$title_news = apply_filters( 'widget_title', $title_news, $instance_news, $this->id_base );
$number_news = ( ! empty( $instance_news['number'] ) ) ? absint( $instance_news['number'] ) : 5;
if ( ! $number_news )
$number_news = 5;
$show_date_news = isset( $instance_news['show_date'] ) ? $instance_news['show_date'] : false;
$r_news = new WP_Query( apply_filters( 'widget_posts_args', array(
'posts_per_page' => $number_news,
'no_found_rows' => true,
'post_status' => 'publish',
'post_type' => array('news',
'ignore_sticky_posts' => true
) ) ) );
if ($r_news->have_posts()) :
?>
<?php echo $args['before_widget']; ?>
<?php if ( $title_news ) {
echo $args['before_title'] . $title_news . $args['after_title'];
} ?>
<ul>
<?php while ( $r_news->have_posts() ) : $r_news->the_post(); ?>
<li>
<?php get_the_title() ? the_title() : the_ID(); ?>
<?php if ( $show_date_news ) : ?>
<span class="post-date"><?php echo get_the_date(); ?></span>
<?php endif; ?>
</li>
<?php endwhile; ?>
</ul>
<?php echo $args['after_widget']; ?>
<?php
wp_reset_postdata();
endif;
if ( ! $this->is_preview() ) {
$cache_news[ $args['widget_id'] ] = ob_get_flush();
wp_cache_set( 'news_widget_recent_posts', $cache_news, 'widget' );
} else {
ob_end_flush();
}
}
public function update( $new_instance, $old_instance_news ) {
$instance_news = $old_instance_news;
$instance_news['title'] = strip_tags($new_instance['title']);
$instance_news['number'] = (int) $new_instance['number'];
$instance_news['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
$this->flush_widget_cache();
$alloptions_news = wp_cache_get( 'alloptions', 'options' );
if ( isset($alloptions_news['News_Recent_Posts']) )
delete_option('News_Recent_Posts');
return $instance_news;
}
public function flush_widget_cache() {
wp_cache_delete('news_widget_recent_posts', 'widget');
}
public function form( $instance_news ) {
$title_news = isset( $instance_news['title'] ) ? esc_attr( $instance_news['title'] ) : '';
$number_news = isset( $instance_news['number'] ) ? absint( $instance_news['number'] ) : 5;
$show_date_news = isset( $instance_news['show_date'] ) ? (bool) $instance_news['show_date'] : false;
?>
<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php echo esc_html__( 'Title:','artor' ); ?></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 $title_news; ?>" /></p>
<p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php echo esc_html__( 'Number of news to show:','artor' ); ?></label>
<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number_news; ?>" size="3" /></p>
<p><input class="checkbox" type="checkbox" <?php checked( $show_date_news ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" />
<label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php echo esc_html__( 'Display news date?' ,'artor'); ?></label></p>
<?php
}
}
function load_widgets() {
register_widget('Movies_Recent_Posts');
register_widget('News_Recent_Posts');
}
add_action( 'widgets_init', 'load_widgets');
Have been fiddling around with the while loop as well as the query params but can't seem to figure out what is causing the following custom code to not limit posts:
/**
* Plugin Name: Latest posts
*/
add_action( 'widgets_init', 'weart_latest_posts' );
function weart_latest_posts() {
register_widget( 'weart_latest_posts' );
}
class weart_latest_posts extends WP_Widget {
//widget setup
function weart_latest_posts() {
$widget_ops = array( 'classname' => 'weart_latest_posts', 'description' => esc_html__('A widget that displays the latest posts.', 'custom') );
$control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => 'weart_latest_posts' );
$this->__construct( 'weart_latest_posts', esc_html__('Custom: Latest Posts Widget', 'custom'), $widget_ops, $control_ops );
}
//display the widget
function widget( $args, $instance ) {
extract( $args );
global $post;
$title = apply_filters('widget_title', $instance['title'] );
$number = $instance['number'];
/* Before widget (defined by themes). */
echo $before_widget;
/* Display the widget title if one was input (before and after defined by themes). */
if ( $title )
echo $before_title . $title . $after_title;
?>
<?php
$args1 = array(
'cat' => 'RECIPES',
'nopaging' => false,
'posts_per_page' => '5',
'found_posts' => '5',
'max_num_pages' => '5',
'post_count' => '5',
'order' => 'DESC',
'orderby' => 'ID',
);
$query = new WP_Query( $args1 );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
;?>
<a href="<?php the_permalink(); ?>" class="block cf item border-top">
<?php if ( has_post_thumbnail()) : ?>
<div class="featured-image col col-one-third img" data-mh="latest_posts_height">
<div class=""><div class="bg lazy" data-src="<?php the_post_thumbnail_url( 'weart-related-thumb'); ?>"></div></div>
</div>
<?php endif; ?>
<div class="text col <?php if ( has_post_thumbnail()) : ?>col-two-third<?php endif; ?>" data-mh="latest_posts_height">
<div class="<?php if ( has_post_thumbnail()) : ?>inner-left text-vertical<?php endif; ?>">
<h3 class="title h4"><?php the_title(); ?></h3>
<div class="meta c-grey link-grey meta-meta"><time datetime="<?php echo esc_attr(get_the_date('Y-m-d')); ?>"> <?php echo esc_attr(get_the_date()); ?></time></div>
</div>
</div>
</a>
<?php }
} else {
} wp_reset_postdata();
?>
<?php
/* After widget (defined by themes). */
echo $after_widget;
}
//update widget
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['number'] = '5';
return $instance;
}
//form for update
function form( $instance ) {
//defaults
$defaults = array( 'title' => esc_html__('Latest Recipes', 'custom'), 'number' => 5, 'popular_days' => 30 );
$instance = wp_parse_args( (array) $instance, $defaults ); ?>
<p><!-- title -->
<label for="<?php echo esc_attr($this->get_field_id( 'title' )); ?>">Title:</label>
<input id="<?php echo esc_attr($this->get_field_id( 'title' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'title' )); ?>" value="<?php echo esc_attr($instance['title']); ?>" style="width:90%;" />
</p><!-- title -->
<p><!-- posts num -->
<label for="<?php echo esc_attr($this->get_field_id( 'number' )); ?>">Number of posts to display:</label>
<input id="<?php echo esc_attr($this->get_field_id( 'number' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'number' )); ?>" value="<?php echo esc_attr($instance['number']); ?>" size="3" />
</p><!-- posts num -->
<?php
}
}
?>
I thought the query params would lead to what I needed and played around with having them both as a variable and also directly in the statement.
Try to remove these:
'nopaging' => false,
'found_posts' => '5',
'max_num_pages' => '5',
'post_count' => '5',
And pass a category ID to this:
'cat' =>
I am green in wordpress.
I have create a widget call woocommerce-dropdown-cart.php and placed in the same folder with the theme I am using. But why I cant find this widget in my Appereance-->widget?
On the other hands, my menu bar have not any widget, how can I add this widget into or just before the menu bar?
Many thanks!
Here is my php code:
<?php class Woocommerce_Dropdown_Cart extends WP_Widget {
public function __construct() {
parent::__construct(
'woocommerce-dropdown-cart', // Base ID
'Woocommerce Dropdown Cart', // Name
array( 'description' => __( 'Woocommerce Dropdown Cart', 'jeffho' ), ) // Args
);
}
public function widget( $args, $instance ) {
global $post;
extract( $args );
echo $before_widget;
global $woocommerce;
global $qode_options_proya;
$cart_holder_class = 'header_cart';
if (isset($qode_options_proya['woo_cart_type'])){
$cart_type = $qode_options_proya['woo_cart_type'];
switch ($cart_type) {
case 'font-elegant':
$cart_holder_class = "header_cart cart_icon";
break;
default:
$cart_holder_class = "header_cart";
break;
}
}
?>
<div class="shopping_cart_outer">
<div class="shopping_cart_inner">
<div class="shopping_cart_header">
<a class="<?php echo esc_attr($cart_holder_class);?>" href="<?php echo $woocommerce->cart->get_cart_url(); ?>"><span class="header_cart_span"><?php echo $woocommerce->cart->cart_contents_count; ?></span></a>
<div class="shopping_cart_dropdown">
<div class="shopping_cart_dropdown_inner">
<?php
$cart_is_empty = sizeof( $woocommerce->cart->get_cart() ) <= 0;
$list_class = array( 'cart_list', 'product_list_widget' );
?>
<ul class="<?php echo implode(' ', $list_class); ?>">
<?php if ( !$cart_is_empty ) : ?>
<?php foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) :
$_product = $cart_item['data'];
// Only display if allowed
if ( ! $_product->exists() || $cart_item['quantity'] == 0 ) {
continue;
}
// Get price
$product_price = get_option( 'woocommerce_tax_display_cart' ) == 'excl' ? $_product->get_price_excluding_tax() : $_product->get_price_including_tax();
$product_price = apply_filters( 'woocommerce_cart_item_price_html', woocommerce_price( $product_price ), $cart_item, $cart_item_key );
?>
<li>
<a itemprop="url" href="<?php echo get_permalink( $cart_item['product_id'] ); ?>">
<?php echo $_product->get_image(); ?>
<?php echo apply_filters('woocommerce_widget_cart_product_title', $_product->get_title(), $_product ); ?>
</a>
<?php echo $woocommerce->cart->get_item_data( $cart_item ); ?>
<?php echo apply_filters( 'woocommerce_widget_cart_item_quantity', '<span class="quantity">' . sprintf( '%s × %s', $cart_item['quantity'], $product_price ) . '</span>', $cart_item, $cart_item_key ); ?>
</li>
<?php endforeach; ?>
<?php else : ?>
<li><?php _e( 'No products in the cart.', 'woocommerce' ); ?></li>
<?php endif; ?>
</ul>
</div>
<?php if ( sizeof( $woocommerce->cart->get_cart() ) <= 0 ) : ?>
<?php endif; ?>
<a itemprop="url" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" class="qbutton white view-cart"><?php _e( 'Cart', 'woocommerce' ); ?> <i class="fa fa-shopping-cart"></i></a>
<span class="total"><?php _e( 'Total', 'woocommerce' ); ?>:<span><?php echo $woocommerce->cart->get_cart_subtotal(); ?></span></span>
<?php if ( sizeof( $woocommerce->cart->get_cart() ) <= 0 ) : ?>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php
echo $after_widget;
}
public function update( $new_instance, $old_instance ) {
$instance = array();
return $instance;
}
}
add_action( 'widgets_init', create_function( '', 'register_widget( "Woocommerce_Dropdown_Cart" );' ) );
?>
<?php
add_filter('add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment');
function woocommerce_header_add_to_cart_fragment( $fragments ) {
global $woocommerce;
ob_start();
?>
<span class="header_cart_span"><?php echo $woocommerce->cart->cart_contents_count; ?></span>
<?php
$fragments['span.header_cart_span'] = ob_get_clean();
return $fragments;
}
?>
I am using theme called "Creativo 5.0" and I have added:
register_sidebar( array(
'name' => __( 'Header Sidebar', 'Creativo' ),
'id' => 'header-sidebar',
'before_widget' => '<div class="header _widget_content">',
'after_widget' => '</div>',
'before_title' => '<h2 class="header-widget-title">',
'after_title' => '</h2>',
) );
into function creativo_widgets_init()
what I want is to add the php widget into the menu bar.
I have facing a problem with pagination in woocomerece.
I am using mystyle theme and with woocomerece plugin.
i want to display 12 product per page.my code is not working
Here is my code.
<?php
$per_page =12;
$numpost = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'product'"); //calculating the number of products
$totalpages= ceil($numpost/$per_page);//calculating the last page or total pages
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
//$page =(isset($_GET['page'])) ? (int)$_GET['page'] : 1 ;
$start=($page-1)* $per_page;
$limit="limit".($page-1)*$per_page.",$per_page";
$args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => $per_page, 'orderby' =>'date','order' => 'DESC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<li class="rcollproli">
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="65px" height="115px" />'; ?>
<h3><?php the_title(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
<?php woo_pagination(); ?>
</li><!-- /span3 -->
<?php endwhile; ?>
<?php
if($totalpages >=1){
for($x=1;$x<=$totalpages;$x++)
{
echo ''.$x.'';
}
}
?>
By default, WooCommerce uses the same "posts per page" setting that is used for blog posts.
But, you can filter it to be any value you like.
add_filter( 'loop_shop_per_page', 'so_27395967_products_per_page' );
function so_27395967_products_per_page(){
return 12;
}
I don't think you need a custom database query.
Here is the solution with code.
global $wpdb;
$per_page =12;
$numpost = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'product'"); //calculating the number of products
//$query= $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE post_type = 'product' orderby ='date'");
$totalpages= ceil($numpost/$per_page);
$page =(isset($_GET['page'])) ? (int)$_GET['page'] : 1 ;
$args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => $per_page,'paged' => get_query_var('paged'), 'orderby' =>'date','order' => 'DESC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<li class="rcollproli">
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="65px" height="115px" />'; ?>
<h3><?php the_title(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
<?php woo_pagination(); ?>
</li><!-- /span3 -->
<?php endwhile; ?>