Hướng dẫn công dụng của buttom load more of wedsite

Bài viết hướng dẫn thêm button "Load More" trong WordPress và tự động tải bài viết khi scroll xuống - Cuộn vô hạn (Infinite Scroll) bằng những đoạn code nhỏ.

Thêm bottom "Load More" - "Xem Thêm"

Bỏ qua bước này nếu bạn muốn tự động trai bài viết khi scroll (cuộn trang)

Chèn đoạn code hiển thị button ngay dưới phân trang của theme TwentySeventeen - index.php (dòng 55), có thể xóa nếu bạn ko cần.

Dùng $wp_query->max_num_pages để kiểm tra đủ bài đăng để hiển thị button hay không.

Note: bài viết sử dụng theme TwentySeventeen.

max_num_pages > 1 ) echo '

More posts
'; // có thể thay thế nó bằng thẻ ?>

Thêm style cho button để trông đẹp hơn

`.misha_loadmore{ background-color:

ddd;

border-radius: 2px; display: block; text-align: center; font-size: 14px; font-size: 0.875rem; font-weight: 800; letter-spacing:1px; cursor:pointer; text-transform: uppercase; padding: 10px 0; transition: background-color 0.2s ease-in-out, border-color 0.2s ease-in-out, color 0.3s ease-in-out; } .misha_loadmore:hover{ background-color:

767676;

color:

fff;

} `

Khai báo file Javascript và tham số AJAX

Thêm đoạn code dưới vào file functions.php trong theme

function misha_my_load_more_scripts() { global $wp_query; //Trong hầu hết các trường hợp, nó đã được đưa vào trang và dòng này có thể bị xóa wp_enqueue_script('jquery'); // đăng ký file javascript wp_register_script( 'my_loadmore', get_stylesheet_directory_uri() . '/myloadmore.js', array('jquery') );

  // chúng ta sẽ khai báo AJAX từ file Javascript đến PHP
wp_localize_script( 'my_loadmore', 'misha_loadmore_params', array(
'ajaxurl' => site_url() . '/wp-admin/admin-ajax.php', // WordPress AJAX
'posts' => json_encode( $wp_query->query_vars ),
'current_page' => get_query_var( 'paged' ) ? get_query_var('paged') : 1,
'max_page' => $wp_query->max_num_pages
) ); wp_enqueue_script( 'my_loadmore' ); } add_action( 'wp_enqueue_scripts', 'misha_my_load_more_scripts' );

Có thể thay thế ajaxurl trên bằng admin_url('admin-ajax.php'), Chú ý nếu không có tham số này bạn sẽ gặp ngay lỗi "ajaxurl is not defined".

Another way is to add a button on the page with a workflow to load more items from your collection every time a user clicks on the button.

Step 1: Add a button

Drag-and-drop a Button on the Canvas or Navigator, wherever you want to display it on the Page.

In the example below, we placed it below our list of calls:

Hướng dẫn công dụng của buttom load more of wedsite

Step 2: Create a variable

This Variable will be called loadMore and of type Number.

Its default value will be the number of items you want to display on the page by default.

In the example below, we want to display 5 items by default.

Hướng dẫn công dụng của buttom load more of wedsite

Step 3: Add a workflow

On the button you created in step 1, add a workflow to Change variable value of the variable you created in step 2.

In the example below, we are saying:

  • when the user clicks on the button,
  • add 5 to the loadMore variable

As a result, every time a user clicks on the button, the loadMore variable will be incremented by 5:

Hướng dẫn công dụng của buttom load more of wedsite

Now, when we bind our Collection List, we need to tell the browser to display the number of items in the loadMore variable we defined in step 2.

In order to do that, we have to slice our list:

Hướng dẫn công dụng của buttom load more of wedsite

WARNING

The slice formula is an Array function so you need to bind to your collection['data']. If you bind to collection, it will not work because you are binding to an Object.

In the screenshot above, we see that:

  • we are binding to a list of items `Number`0 instead of the entire collection object `Number`1
  • we are using the slice no-code formula to display the items from index 0 to the index X
  • X changes based on the value of the loadMore variable, which changes every time the user clicks on the button (see step 3)

Step 5: Test in Preview

Ok, that should work.

Back in Preview mode, you can click on the button to see the loadMore variable be updated in the Navigator and the additional items displayed on the page: