About
This plugin is developed for a new wordpress site from UMH. This website will display lists of different post types on the same page.
A demo of this plugin can be found in test-kadence/load-more-test/
This plugin supports the following operations:
- Specify the name of custom post type.
- Specify the post per page (ppp).
- Initially loads ppp number of posts with a ‘Load More’ button under it.
- By clicking the button, more posts will be loaded into the list.
- The button is hidden when no more posts to load.
- The post content in a list is defined in a template function. Template functions have to be developed for individual post types.
- The custom post type should have a meta data “sort-rank”. The plugin sort the posts based on the sort-rank value in ascending order.
How to Add a New Post List
- Edit inc/template-functions.php as follows:
- Inside the function lmp_template($post, $ptype), add a ‘switch – case’ for the new post type and ‘return’ the result of the template function for this post type.
- Create a new template function which returns the html for the browser display.
- Edit assets/scss/ivr-lmp-style.scss to include the styling required for the post type.
- After editing, compile this scss file to generate the css file.
- Add the following shortcode at the appropriate location of the page:
Shortcode: [ ivr_load_more_posts post_type=”post type name” ppp=”posts per page” ]
Key Operations
- At the ‘ini’ hook, registers ‘ivr-lmp-style.css’ and ‘ivr-lmp-script.js’.
- At the ‘wp_enqueue_scripts’ hook, enqueue the above css and js files.
- A conditional check in the hook function is run to only enqueue the files on the required page to avoid unnecessary loading time for other pages.
- At the ‘wp_ajax_nopriv_[action]’ hook, run the function lmp_ajax_load_posts().
- At the ‘wp_ajax_[action]’ hook, run the function lmp_ajax_load_posts().
- Register the shortcode using the function add_shortcode( ‘ivr_load_more_posts’, ‘ivr_load_more_posts_fun’ ).
Points to Note
- For the php function to handle the action called by Ajax request, use ‘echo’ to return the response to either the Ajax’s success or error functions. Do not use ‘return’ as this won’t work.
- For the php function to handle the action called by Ajax request, it must be ended with the command die( ) to close the Ajax connection properly. Otherwise PHP will append a ‘0’ to the end of the response to signal the problem.
- In html, the data attributes do not support uppercase characters. Use small case characters only.
- data-name =”…” –> ok
- data-Name = “…” –> not ok
- In the registration of the js script, by specifying jquery in the dependency array can load the core’s jquery file. For example:
- wp_register_script( ‘lmp_script’, plugins_url (‘/assets/js/ivr-lmp-script.js’, __DIR__), array(‘jquery’), ‘1.0.0’, true);
Folders and Files
ivr-load-more-posts.php
- the main file of the plugin. It triggers the required functions at the various action hooks mentioned above.
uninstall.php
- Currently do nothing. It is created for completeness for the plugin.
inc/loader.php
- when called, it loads in all files in the ‘inc’ folder.
- but excludes itself and index.php
inc/ivr-lmp-functions.php
- defines all functions called by ivr-load-more-posts.php
inc/ajax.php
- defines a php function to handle the ajax request and echo a response.
inc/template-functions.php
- defines the “switch / case” function to decide which template function to call for a particular post type.
- defines template functions for outputting the html of a particular post type.
assets/scss/ivr-lmp-style.scss
- the main SCSS file.
assets/scss/_ivr-lmp-tmpl-book.scss
- the partial SCSS file for the Book template function.
assets/scss/_ivr-lmp-tmpl-workstation.scss
- the partial SCSS file for the Workstation template function.
assets/css/ivr-lmp-style.css
- the auto-generated CSS file after compilation of the SCSS file.
assets/css/ivr-lmp-style.css.map
- the auto-generated resource map file after compilation of the SCSS file. This map file enables the browser dev tool to show which SCSS file is providing the styling on a particular elements.
assets/js/ivr-lmp-script.js
- the main JS & jQuery file.
Version History & Download
| Ver. | Rel Date | Remark |
| 1.0.2 | 2021-10-02 | – JS file line 24 amended ‘respose’ to ‘response’. – In assest folder, removed the ‘index copy.php’ |
| 1.0.1 | 2021-04-23 | Minor improvements |
| 1.0.0 | 2021-04-19 | First Release |
