با سلام، در این مطلب به ادامه آموزش ساخت Testimonial در وردپرس قسمت اول خواهیم پرداخت آموزش خواهیم داد که چگونه برای Testimonial سورت کد ایجاد و از آن ها در قسمت های مختلف استفاده کرد .لازم به ذکر است که کد های کوتاه در وردپرس بسیار مفید و کاربرادی است پس برای این کار هم می توان سورت کد ایجاد و استفاده کرد با ما همراه باشید …
برای این کارابتدا باید از تابع زیر استفاده کرد.
add_shortcode( 'testimonial', 'testimonial_shortcode' ); /** * ساخت شورت کد * */ function testimonial_shortcode( $atts ) { extract( shortcode_atts( array( 'posts_per_page' => '1', 'orderby' => 'none', 'testimonial_id' => '', ), $atts ) ); return get_testimonial( $posts_per_page, $orderby, $testimonial_id ); }
حال نوبت به استفاده از شورت کد زیر در هر کجا که دوست دارید می باشد.
[testimonial posts_per_page="1" orderby="none" testimonial_id=""]
خب حال نوبت به ایجاد و ساخت ابزارک برای Testimonials می باشد که برای این کار از کد زیر استفاده کنید.
class Testimonial_Widget extends WP_Widget { public function __construct() { $widget_ops = array( 'classname' => 'testimonial_widget', 'description' => 'نمایش دیدگاه های مشتریان' ); parent::__construct( 'testimonial_widget', 'دیدگاه های مشتریان', $widget_ops ); } public function widget( $args, $instance ) { extract( $args ); $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base ); $posts_per_page = (int) $instance['posts_per_page']; $orderby = strip_tags( $instance['orderby'] ); $testimonial_id = ( null == $instance['testimonial_id'] ) ? '' : strip_tags( $instance['testimonial_id'] ); echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; echo get_testimonial( $posts_per_page, $orderby, $testimonial_id ); echo $after_widget; } public function update( $new_instance, $old_instance ) { $instance = $old_instance; $instance['title'] = strip_tags( $new_instance['title'] ); $instance['posts_per_page'] = (int) $new_instance['posts_per_page']; $instance['orderby'] = strip_tags( $new_instance['orderby'] ); $instance['testimonial_id'] = ( null == $new_instance['testimonial_id'] ) ? '' : strip_tags( $new_instance['testimonial_id'] ); return $instance; } public function form( $instance ) { $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'posts_per_page' => '1', 'orderby' => 'none', 'testimonial_id' => null ) ); $title = strip_tags( $instance['title'] ); $posts_per_page = (int) $instance['posts_per_page']; $orderby = strip_tags( $instance['orderby'] ); $testimonial_id = ( null == $instance['testimonial_id'] ) ? '' : strip_tags( $instance['testimonial_id'] ); ?> <label for="<?php echo $this->get_field_id( '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 ); ?>" /> <label for="<?php echo $this->get_field_id( 'posts_per_page' ); ?>">تعداد دیدگاه ها: </label> <input class="widefat" id="<?php echo $this->get_field_id( 'posts_per_page' ); ?>" name="<?php echo $this->get_field_name( 'posts_per_page' ); ?>" type="text" value="<?php echo esc_attr( $posts_per_page ); ?>" /> <label for="<?php echo $this->get_field_id( 'orderby' ); ?>">مرتب سازی براساس</label> <select id="<?php echo $this->get_field_id( 'orderby' ); ?>" name="<?php echo $this->get_field_name( 'orderby' ); ?>"> <option value="none" <?php selected( $orderby, 'none' ); ?>>هیچ</option> <option value="ID" <?php selected( $orderby, 'ID' ); ?>>ID</option> <option value="date" <?php selected( $orderby, 'date' ); ?>>تاریخ</option> <option value="modified" <?php selected( $orderby, 'modified' ); ?>>آخرین تغییر</option> <option value="rand" <?php selected( $orderby, 'rand' ); ?>>تصادفی</option> </select> <label for="<?php echo $this->get_field_id( 'testimonial_id' ); ?>">دیدگاه ID</label> <input class="widefat" id="<?php echo $this->get_field_id( 'testimonial_id' ); ?>" name="<?php echo $this->get_field_name( 'testimonial_id' ); ?>" type="text" value="<?php echo $testimonial_id; ?>" /> <?php } } add_action( 'widgets_init', 'register_testimonials_widget' ); /** * ثبت ابزارک */ function register_testimonials_widget() { register_widget( 'Testimonial_Widget' ); }
به پایان قسمت دوم آموزش ساخت Testimonials در وردپرس رسیدیم امید واریم توانسه باشیم با لحنی ساده و راحت توضیح داده باشم و مورد پسند شما واقع قرار گرفته باشد.
قسمت اول آموزش ساخت Testimonials در وردپرس
منبع: وردپرس