Do pobrania tutaj. Plik można dołączyć do motywu, lub jego zawartość wkleić bezpośrednio do functions.php.
Widget zawiera pola takie jak:
Na samym dole po komentarzu:
1 |
<?php /** Custom widget code */ ?> |
możemy wyedytować kod html, według własnych potrzeb.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
<?php /** DKiT: Sidebar Custom Posts */ ?> <?php /** Register and load the widget */ function dkit_scp_load_widget() { register_widget('dkit_scp_widget'); } add_action('widgets_init', 'dkit_scp_load_widget'); /** Creating the widget */ class dkit_scp_widget extends WP_Widget { public function __construct() { $widget_options = array( 'classname' => 'dkit-scp-widget', 'description' => 'Wyświetla niestandardowe posty na pasku bocznym', // Widget description ); parent::__construct( 'dkit-scp-widget', // Base ID of your widget 'DKiT: Sidebar Custom Posts', // Widget name will appear in UI $widget_options ); } /** Creating widget back-end */ public function form($instance) { $widget_title = ! empty($instance['widget_title']) ? $instance['widget_title'] : ''; $widget_layout = ! empty($instance['widget_layout']) ? $instance['widget_layout'] : ''; $posts_quantity = ! empty($instance['posts_quantity']) ? $instance['posts_quantity'] : '3'; $post_order = ! empty($instance['post_order']) ? $instance['post_order'] : ''; $excerpt_switch = ! empty($instance['excerpt_switch']) ? $instance['excerpt_switch'] : ''; $excerpt_length = ! empty($instance['excerpt_length']) ? $instance['excerpt_length'] : '10'; $button_read_more_switch = ! empty($instance['button_read_more_switch']) ? $instance['button_read_more_switch'] : ''; ?> <?php // widget_title ?> <p> <label for="<?php echo $this->get_field_id('widget_title'); ?>">Tytuł:</label> <input class="widefat" id="<?php echo $this->get_field_id('widget_title'); ?>" name="<?php echo $this->get_field_name('widget_title'); ?>" type="text" value="<?php echo esc_attr($widget_title); ?>" /> </p> <?php // widget_layout ?> <p> <label for="<?php echo $this->get_field_id('widget_layout'); ?>">Układ: <select class='widefat' id="<?php echo $this->get_field_id('widget_layout'); ?>" name="<?php echo $this->get_field_name('widget_layout'); ?>" type="text"> <option value='vertical'<?php echo ($widget_layout=='vertical')?'selected':''; ?>> Pionowy </option> <option value='horizontal'<?php echo ($widget_layout=='horizontal')?'selected':''; ?>> Poziomy </option> </select> </label> </p> <?php // posts_quantity ?> <p> <label for="<?php echo $this->get_field_id('posts_quantity'); ?>">Ilość postów:</label> <input class="widefat" id="<?php echo $this->get_field_id('posts_quantity'); ?>" name="<?php echo $this->get_field_name('posts_quantity'); ?>" type="number" min="1" value="<?php echo esc_attr($posts_quantity); ?>" /> </p> <?php // post_order ?> <p> <label for="<?php echo $this->get_field_id('post_order'); ?>">Sortowanie: <select class='widefat' id="<?php echo $this->get_field_id('post_order'); ?>" name="<?php echo $this->get_field_name('post_order'); ?>" type="text"> <option value='DESC'<?php echo ($post_order=='DESC')?'selected':''; ?>> Malejąco </option> <option value='ASC'<?php echo ($post_order=='ASC')?'selected':''; ?>> Rosnąco </option> <option value='random'<?php echo ($post_order=='random')?'selected':''; ?>> Losowo </option> </select> </label> </p> <?php // excerpt ?> <?php // excerpt_switch ?> <p class="checkbox-parent"> <input class="checkbox" type="checkbox" <?php if (isset($instance[ 'excerpt_switch' ])) : checked( $instance[ 'excerpt_switch' ], 'on' ); endif; ?> id="<?php echo $this->get_field_id( 'excerpt_switch' ); ?>" name="<?php echo $this->get_field_name( 'excerpt_switch' ); ?>" /> <label for="<?php echo $this->get_field_id( 'excerpt_switch' ); ?>">Zajawka</label> </p> <?php // excerpt_length ?> <p class="checkbox-on"> <label for="<?php echo $this->get_field_id('excerpt_length'); ?>">Długość zajawki (ilość słów):</label> <input class="widefat" id="<?php echo $this->get_field_id('excerpt_length'); ?>" name="<?php echo $this->get_field_name('excerpt_length'); ?>" type="number" min="1" value="<?php echo esc_attr($excerpt_length); ?>" /> </p> <?php // button_read_more ?> <?php // button_read_more_switch ?> <p> <input class="checkbox" type="checkbox" <?php if (isset($instance[ 'button_read_more_switch' ])) : checked( $instance[ 'button_read_more_switch' ], 'on' ); endif; ?> id="<?php echo $this->get_field_id( 'button_read_more_switch' ); ?>" name="<?php echo $this->get_field_name( 'button_read_more_switch' ); ?>" /> <label for="<?php echo $this->get_field_id( 'button_read_more_switch' ); ?>">Przycisk "Czytaj więcej"</label> </p> <?php // checkbox management ?> <script> (function($) { $('.checkbox-parent .checkbox').parent('.checkbox-parent').next('.checkbox-on').css({ 'display': 'none', }); $('.checkbox-parent .checkbox:checked').parent('.checkbox-parent').next('.checkbox-on').css({ 'display': 'block', }); $('.checkbox-parent .checkbox').click(function(){ if($(this).is(':checked')) { $(this).parent('.checkbox-parent').next('.checkbox-on').fadeIn('fast'); } else { $(this).parent('.checkbox-parent').next('.checkbox-on').fadeOut('fast'); } }); })(jQuery); </script> <?php } /** Updating widget replacing old instances with new */ public function update($new_instance, $old_instance) { $instance = array(); $instance['widget_title'] = (!empty($new_instance['widget_title'])) ? strip_tags($new_instance['widget_title']) : ''; $instance['widget_layout'] = (!empty( $new_instance['widget_layout'])) ? strip_tags($new_instance['widget_layout']) : ''; $instance['posts_quantity'] = (!empty( $new_instance['posts_quantity'])) ? strip_tags($new_instance['posts_quantity']) : ''; $instance['post_order'] = (!empty( $new_instance['post_order'])) ? strip_tags($new_instance['post_order']) : ''; $instance['excerpt_switch'] = (!empty( $new_instance['excerpt_switch'])) ? strip_tags($new_instance['excerpt_switch']) : ''; $instance['excerpt_length'] = (!empty( $new_instance['excerpt_length'])) ? strip_tags($new_instance['excerpt_length']) : '10'; $instance['button_read_more_switch'] = (!empty( $new_instance['button_read_more_switch'])) ? strip_tags($new_instance['button_read_more_switch']) : ''; return $instance; } /** Creating widget front-end */ public function widget($args, $instance) { // post_image_size if ($instance['widget_layout'] == 'vertical') { $post_image_size = 'sidebar-post-lg'; } elseif ($instance['widget_layout'] == 'horizontal') { $post_image_size = 'sidebar-post-sm'; } // get widget title $widget_title = $instance['widget_title']; // get and set excerpt length $new_excerpt_length = $instance['excerpt_length']; add_filter('excerpt_length', function() use($new_excerpt_length) { $excerpt = $new_excerpt_length; return $excerpt; }); // replace default readMore function (functions.php) add_filter('excerpt_more', function() { return; }); // before_widget echo $args['before_widget']; ?> <?php /** Custom widget code */ ?> <?php if (!empty($widget_title)) : ?> <h4 class="widget-title"><?php echo $widget_title; ?></h4> <?php endif ?> <?php $count = 0; $new_posts_quantity = $instance['posts_quantity']; $new_post_order = $instance['post_order']; $new_post_orderby = ''; if ($new_post_order == 'random') { $new_post_orderby = 'rand'; } else { $new_post_orderby = 'date'; } $widget_args = array( 'posts_per_page' => $new_posts_quantity, 'orderby' => $new_post_orderby, 'order' => $new_post_order, ); $widget_query = new WP_Query($widget_args); ?> <?php // post list ?> <ul<?php if ($instance['widget_layout'] == 'horizontal') : echo ' class="widget-horizontal"'; endif; ?>> <?php while($widget_query->have_posts()) : $widget_query->the_post(); $count++; ?> <?php $post_title = get_the_title(); ?> <li> <a href="<?php the_permalink(); ?>" title="<?php echo wp_strip_all_tags($post_title); ?>"> <div class="img-area"> <?php if (has_post_thumbnail()) : the_post_thumbnail($post_image_size, array('class' => 'img-fluid')); endif; ?> </div> <div class="widget-post-title"><?php echo $post_title; ?></div> </a> <?php if ($instance['excerpt_switch'] == true) : ?> <div class="excerpt"> <?php $product = get_the_ID(); $readMore = '<a href="' . get_the_permalink() . '" title="' . __('Read more', 'nanobrow_base') . '" class="btn btn-primary">'; $readMore .= __('Read more', 'nanobrow_base'); $readMore .= '</a>'; if ( get_post_meta( $product, 'dkit_new_excerpt', true ) && get_post_meta( $product, 'dkit_accept_content_replacement', true )) : $output = get_post_meta( $product, 'dkit_new_excerpt', true ); $output .= $readMore; echo $output; elseif ( get_post_meta( $product, 'excerpt_custom', true ) && get_post_meta( $product, 'dkit_accept_content_replacement', false )) : $output = get_post_meta( $product, 'excerpt_custom', true ); $output .= $readMore; echo $output; else: the_excerpt(); endif; ?> </div> <?php endif; ?> <?php if ($instance['button_read_more_switch'] == true) : ?> <a href="<?php the_permalink(); ?>" title="<?php _e('Read More', 'nanobrow_base'); ?>"><?php _e('Read More', 'nanobrow_base'); ?></a> <?php endif; ?> </li> <?php endwhile; ?> </ul> <?php // after_widget echo $args['after_widget']; } } |