Сегодня мне довелось редактировать плагин WP-Property ,а конкретней , изменить навигацию с слайдера на цифровую пагинацию
И естественно я хочу с вами поделится как у меня это вышло 🙂
Но с начало немного пред истории…
Несмотря на то что сам плагин достаточно мощный, и пользуется спросом, разработчики в упор не хотят помочь своим пользователям в этой проблеме(изменить вид пагинации),хоть и имеет массу записей в фидбеке по этому поводу…
Ну довольно воды,перейдем к делу 🙂
Конечный результат выглядит так же как и на этом сайте http://mihalev.ru/doma/
Правки будем делать в двух файлах это wp_properties.css и template-functions.php , оба файла находятся в папке templates самого плагина
Сначала вставляем в файл wp_properties.css ниже приведенный код
1 2 3 4 5 6 7 8 | .wpp_pagination_slider_i a.active, .wpp_pagination_slider_i a:hover { color: #f00; } .wpp_pagination_slider_i a { display: block; float: left; margin-left: 5px; } |
теперь в файле template-functions.php меняем полностью основной js скрипт после строк
if ( $settings[ ‘javascript’ ] ) {
ob_start();?>
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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | <script>// <![CDATA[ /* * The functionality below is used for pagination and sorting the list of properties * It can be many times (on multiple shortcodes) * So the current javascript functionality should not to be initialized twice. */ /* * Init global WPP_QUERY variable which will contain all query objects */ if ( typeof wpp_query == 'undefined' ) { var wpp_query = []; } /* * */ if ( typeof document_ready == 'undefined' ) { var document_ready = false; } /* * Initialize shortcode's wpp_query object */ if ( typeof wpp_query_<?php echo $unique_hash; ?> == 'undefined' ) { var wpp_query_<?php echo $unique_hash; ?> = <?php echo json_encode($wpp_query); ?>; /* Default values for ajax query. It's used when we go to base URL using back button */ wpp_query_<?php echo $unique_hash; ?>['default_query'] = wpp_query_<?php echo $unique_hash; ?>.query; /* Push query objects to global wpp_query variable */ wpp_query.push( wpp_query_<?php echo $unique_hash; ?> ); } /* * Init variable only at once */ if ( typeof wpp_pagination_history_ran == 'undefined' ) { var wpp_pagination_history_ran = false; } /* Init variable only at once */ if ( typeof wpp_pagination_<?php echo $unique_hash; ?> == 'undefined' ) { var wpp_pagination_<?php echo $unique_hash; ?> = false; } if ( typeof first_load == 'undefined' ) { var first_load = true; } /* Watch for address URL for back buttons support */ if ( !wpp_pagination_history_ran ) { wpp_pagination_history_ran = true; /* * On change location (address) Event. * * Also used as Back button functionality. * * Attention! This event is unique (binds at once) and is used for any (multiple) shortcode */ jQuery( document ).ready( function () { if ( !jQuery.isFunction( jQuery.fn.slider ) ) { return; } jQuery.address.change( function ( event ) { callPagination( event ); } ); } ); /* * Parse location (address) hash, * Setup shortcode params by hash params * Calls ajax pagination */ function callPagination ( event ) { /* * We have to be sure that DOM is ready * if it's not, wait 0.1 sec and call function again */ if ( !document_ready ) { window.setTimeout( function () { callPagination( event ); }, 100 ); return false; } var history = {}; /* Parse hash value (params) */ var hashes = event.value.replace( /^\//, '' ); /* Determine if we have hash params */ if ( hashes ) { hashes = hashes.split( '&' ); for ( var i in hashes ) { if ( typeof hashes[i] != 'function' ) { hash = hashes[i].split( '=' ); history[hash[0]] = hash[1]; } } if ( history.i ) { /* get current shortcode's object */ var index = parseInt( history.i ) - 1; if ( index >= 0 ) { var q = wpp_query[index]; } if ( typeof q == 'undefined' || q.length == 0 ) { //ERROR return false; } if ( history.sort_by && history.sort_by != '' ) { q.sort_by = history.sort_by; } if ( history.sort_order && history.sort_order != '' ) { q.sort_order = history.sort_order; } /* 'Select/Unselect' sortable buttons */ var sortable_links = jQuery( '#wpp_shortcode_' + q.unique_hash + ' .wpp_sortable_link' ); if ( sortable_links.length > 0 ) { sortable_links.each( function ( i, e ) { jQuery( e ).removeClass( "wpp_sorted_element" ); if ( jQuery( e ).attr( 'sort_slug' ) == q.sort_by ) { jQuery( e ).addClass( "wpp_sorted_element" ); } } ); } if ( history.requested_page && history.requested_page != '' ) { eval( 'wpp_do_ajax_pagination_' + q.unique_hash + '(' + history.requested_page + ')' ); } else { eval( 'wpp_do_ajax_pagination_' + q.unique_hash + '(1)' ); } } else { return false; } } else { /* Looks like it's base url * Determine if this first load, we do nothing * If not, - we use 'back button' functionality. */ if ( first_load ) { first_load = false; } else { /* * Set default pagination values for all shortcodes */ for ( var i in wpp_query ) { wpp_query[i].sort_by = wpp_query[i].default_query.sort_by; wpp_query[i].sort_order = wpp_query[i].default_query.sort_order; /* 'Select/Unselect' sortable buttons */ var sortable_links = jQuery( '#wpp_shortcode_' + wpp_query[i].unique_hash + ' .wpp_sortable_link' ); if ( sortable_links.length > 0 ) { sortable_links.each( function ( ie, e ) { jQuery( e ).removeClass( "wpp_sorted_element" ); if ( jQuery( e ).attr( 'sort_slug' ) == wpp_query[i].sort_by ) { jQuery( e ).addClass( "wpp_sorted_element" ); } } ); } eval( 'wpp_do_ajax_pagination_' + wpp_query[i].unique_hash + '(1, false)' ); } } } } } /* * Changes location (address) hash based on pagination * * We use this function extend of wpp_do_ajax_pagination() * because wpp_do_ajax_pagination() is called on change Address Value's event * * @param int this_page Page which will be loaded * @param object data WPP_QUERY object * @return object data Returns updated WPP_QUERY object */ if ( typeof changeAddressValue == 'undefined' ) { function changeAddressValue ( this_page, data ) { var q = window.wpp_query; /* Get the current shortcode's index */ var index = 0; for ( var i in q ) { if ( q[i].unique_hash == data.unique_hash ) { index = (++i); break; } } /* Set data query which will be used in history hash below */ var q = { requested_page: this_page, sort_order: data.sort_order, sort_by: data.sort_by, i: index }; /* Update WPP_QUERY query */ data.query.requested_page = this_page; data.query.sort_order = data.sort_order; data.query.sort_by = data.sort_by; /* * Update page URL for back-button support (needs to do sort order and direction) * jQuery.address.value() and jQuery.address.path() double binds jQuery.change() event, some way * so for now, we use window.location */ var history = jQuery.param( q ); window.location.hash = '/' + history; return data; } } function wpp_do_ajax_pagination_<?php echo $unique_hash; ?>( this_page, scroll_to ) { if ( typeof this_page == 'undefined' ) { this_page = 1; } if ( typeof scroll_to == 'undefined' ) { scroll_to = true; } data = wpp_query_<?php echo $unique_hash; ?>; /* Update page counter */ jQuery( "#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_current_page_count" ).text( this_page ); // jQuery( "#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_pagination_slider_i .active .slider_page_info .val" ).text( this_page ); /* Update sliders */ //jQuery( "#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_pagination_slider_i" ).slider( "value", this_page ); jQuery( '#wpp_shortcode_<?php echo $unique_hash; ?> .ajax_loader' ).show(); /* Scroll page to the top of the current shortcode */ if ( scroll_to ) { jQuery( document ).trigger( 'wpp_pagination_change', {'overview_id': <?php echo $unique_hash; ?>} ); } data.ajax_call = 'true'; data.requested_page = this_page; jQuery.post( '<?php echo admin_url('admin-ajax.php'); ?>', { action: 'wpp_property_overview_pagination', wpp_ajax_query: data }, function ( result_data ) { jQuery( '#wpp_shortcode_<?php echo $unique_hash; ?> .ajax_loader' ).hide(); var p_list = jQuery( '.wpp_property_view_result', result_data.display ); //* Determine if p_list is empty try previous version's selector */ if ( p_list.length == 0 ) { p_list = jQuery( '.wpp_row_view', result_data.display ); } var content = ( p_list.length > 0 ) ? p_list.html() : result_data.display; var p_wrapper = jQuery( '#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_property_view_result' ); //* Determine if p_wrapper is empty try previous version's selector */ if ( p_wrapper.length == 0 ) { p_wrapper = jQuery( '#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_row_view' ) } p_wrapper.html( content ); /* Total properties count may change depending on sorting (if sorted by an attribute that all properties do not have) */ /* It seems issue mentioned above are fexed so nex line unneeded, commented odokienko@UD */ // jQuery("#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_property_results").text(result_data.wpp_query.properties?result_data.wpp_query.properties.total:0); <?php if($use_pagination) { ?> /* Update max page in slider and in display */ // jQuery( "#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_pagination_slider" ).slider( "option", "max", result_data.wpp_query.pages ); jQuery( "#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_total_page_count" ).text( result_data.wpp_query.pages ); max_slider_pos = result_data.wpp_query.pages; if ( max_slider_pos == 0 ) jQuery( "#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_current_page_count" ).text( 0 ); <?php } ?> jQuery( "#wpp_shortcode_<?php echo $unique_hash; ?> a.fancybox_image" ).fancybox( { 'transitionIn': 'elastic', 'transitionOut': 'elastic', 'speedIn': 600, 'speedOut': 200, 'overlayShow': false } ); jQuery( document ).trigger( 'wpp_pagination_change_complete', {'overview_id': <?php echo $unique_hash; ?>} ); }, "json" ); } jQuery( document ).ready( function () { if ( !jQuery.isFunction( jQuery.fn.slider ) || !jQuery.isFunction( jQuery.fn.slider ) ) { jQuery( ".wpp_pagination_slider_wrapper" ).hide(); return null; } document_ready = true; max_slider_pos = <?php echo ($pages ? $pages : 'null'); ?>; //** Do not assign click event again */ if ( !jQuery( '#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_pagination_forward' ).data( 'events' ) ) { jQuery( '#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_pagination_forward' ).on( 'click',function () { var valuey = jQuery("#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_pagination_slider_i .active").attr("page"); if ( max_slider_pos && (parseInt(valuey) == max_slider_pos || max_slider_pos < 1 ) ) { return; } var new_value = parseInt(valuey)+1; jQuery( "#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_pagination_slider_i a" ).removeClass('active'); jQuery( "#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_pagination_slider_i .id"+new_value ).addClass('active'); wpp_query_<?php echo $unique_hash; ?> = changeAddressValue( new_value, wpp_query_<?php echo $unique_hash; ?> ); jQuery( "#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_current_page_count" ).html( new_value ); }); } if ( !jQuery( '#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_pagination_back' ).data( 'events' ) ) { jQuery( '#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_pagination_back' ).on( 'click',function () { var value = jQuery( "#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_pagination_slider_i .active" ).attr('page'); if ( parseInt(value) == 1 ) { return; } var new_value = parseInt(value) - 1; jQuery( "#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_pagination_slider_i a" ).removeClass('active'); jQuery( "#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_pagination_slider_i .id"+new_value ).addClass('active'); wpp_query_<?php echo $unique_hash; ?> = changeAddressValue( new_value, wpp_query_<?php echo $unique_hash; ?> ); jQuery( "#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_current_page_count" ).html( new_value ); }); } jQuery( '#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_pagination_slider_i a' ).on( 'click',function (event) { event.preventDefault(); var value = jQuery( this).attr('page'); if ( parseInt(value) == 1 ) { return; } jQuery( "#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_pagination_slider_i a" ).removeClass('active'); jQuery( "#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_pagination_slider_i .id"+value ).addClass('active'); wpp_query_<?php echo $unique_hash; ?> = changeAddressValue( value, wpp_query_<?php echo $unique_hash; ?> ); jQuery( "#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_current_page_count" ).html( value ); }); if ( !jQuery( '#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_sortable_link' ).data( 'events' ) ) { jQuery( '#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_sortable_link' ).click( function () { var attribute = jQuery( this ).attr( 'sort_slug' ); var sort_order = jQuery( this ).attr( 'sort_order' ); var this_attribute = jQuery( "#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_sortable_link[sort_slug=" + attribute + "]" ); if ( jQuery( this ).is( ".wpp_sorted_element" ) ) { var currently_sorted = true; /* If this attribute is already sorted, we switch sort order */ if ( sort_order == "ASC" ) { sort_order = "DESC"; } else if ( sort_order == "DESC" ) { sort_order = "ASC"; } } jQuery( "#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_sortable_link" ).removeClass( "wpp_sorted_element" ); wpp_query_<?php echo $unique_hash; ?>.sort_by = attribute; wpp_query_<?php echo $unique_hash; ?>.sort_order = sort_order; jQuery( this_attribute ).addClass( "wpp_sorted_element" ); jQuery( this_attribute ).attr( "sort_order", sort_order ); /* Get ajax results and reset to first page */ wpp_query_<?php echo $unique_hash; ?> = changeAddressValue( 1, wpp_query_<?php echo $unique_hash; ?> ); } ); } if ( !jQuery( '#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_sortable_dropdown' ).data( 'events' ) ) { jQuery( '#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_sortable_dropdown' ).change( function () { var parent = jQuery( this ).parents( '.wpp_sorter_options' ); var attribute = jQuery( ":selected", this ).attr( 'sort_slug' ); var sort_element = jQuery( ".sort_order", parent ); var sort_order = jQuery( sort_element ).attr( 'sort_order' ); wpp_query_<?php echo $unique_hash; ?>.sort_by = attribute; wpp_query_<?php echo $unique_hash; ?>.sort_order = sort_order; /* Get ajax results and reset to first page */ wpp_query_<?php echo $unique_hash; ?> = changeAddressValue( 1, wpp_query_<?php echo $unique_hash; ?> ); } ); } if ( !jQuery( '#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_overview_sorter' ).data( 'events' ) ) { jQuery( '#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_overview_sorter' ).click( function () { var parent = jQuery( this ).parents( '.wpp_sorter_options' ); var sort_element = this; var dropdown_element = jQuery( ".wpp_sortable_dropdown", parent ); var attribute = jQuery( ":selected", dropdown_element ).attr( 'sort_slug' ); var sort_order = jQuery( sort_element ).attr( 'sort_order' ); jQuery( sort_element ).removeClass( sort_order ); /* If this attribute is already sorted, we switch sort order */ if ( sort_order == "ASC" ) { sort_order = "DESC"; } else if ( sort_order == "DESC" ) { sort_order = "ASC"; } wpp_query_<?php echo $unique_hash; ?>.sort_by = attribute; wpp_query_<?php echo $unique_hash; ?>.sort_order = sort_order; jQuery( sort_element ).attr( "sort_order", sort_order ); jQuery( sort_element ).addClass( sort_order ); /* Get ajax results and reset to first page */ wpp_query_<?php echo $unique_hash; ?> = changeAddressValue( 1, wpp_query_<?php echo $unique_hash; ?> ); } ); } <?php if($use_pagination) { ?> jQuery( "#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_pagination_slider_wrapper" ).each( function () { var this_parent = this; /* Slider */ jQuery( '.wpp_pagination_slider', this ).slider( { value: 1, min: 1, max: <?php echo $pages; ?>, step: 1, slide: function ( event, ui ) { jQuery( "#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_current_page_count" ).text( ui.value ); //jQuery( "#wpp_shortcode_<?php echo $unique_hash; ?> .wpp_pagination_slider .slider_page_info .val" ).text( ui.value ); }, stop: function ( event, ui ) { //wpp_query_<?php echo $unique_hash; ?> = changeAddressValue( ui.value, wpp_query_<?php echo $unique_hash; ?> ); } } ); /* Fix slider width based on button width */ var slider_width = (jQuery( this_parent ).width() - jQuery( ".wpp_pagination_back", this_parent ).outerWidth() - jQuery( ".wpp_pagination_forward", this_parent ).outerWidth() - 30); //jQuery( ".wpp_pagination_slider", this_parent ).css( 'width', slider_width ); //jQuery( '.wpp_pagination_slider .ui-slider-handle', this ).append( ' <div class="slider_page_info"> <div class="val">1</div> <div class="arrow"></div> </div> ' ); } ); <?php } ?> } ); // ]]></script> |
И теперь в этом же файле ищем код
И меняем его на
1 2 3 4 5 6 7 |
На этом все,оставляйте ваши вопросы в комментариях, и подписывайтесь на меня в соц сетях , ждите новых статей
И пару тегов для наших англоязычных друзей
WP-Property use digital pagination instead of slider