The ACF Recent Posts Widget has just been improved. One can now specify custom templates per each theme used overwriting the default plugin behavior!
Creating Custom Templates
Great resource from version 4.3 the plugin supports custom templates. To make usage of these one needs to make a copy of all the files found in the acf-recent-posts-widget/templates directory and copy them over to the active template directory under acfrpw subdirectory. The approach is similar to the way WooCommerce plugin works and has it’s drawbacks. With every new feature the files may be outdated.
There are 3 main template files:
- loop-after.php (which displays the markup after each of the posts and closes the markup container)
- loop-before.php (which displays the markup before each of the posts and opens the markup container)
- loop-inner.php (which is enqueued for each of the posts separatelly and contains the markup of every single post)
Usage of the templates
The usage is quite advanced hence each template file contains a quite detailed documentation on how to use it.
First of all the template files mustn’t have the global variables removed. Each of these variables stores the widget settings, which are then used to generate the code. Second of all the variable names are unobvious, loop-inner.php template file contains a list of all the names used, which are then extracted and available as php variables.
Different template per widget / sidebar
The templates allow one to adjust the markup of each single widget. One needs first to verify the widget id used, which may then be referenced. The variable which stores the widget id is $acf_rpw_args[‘widget_id’]. Dumping the value in the template is the best way to find out which automatic id has been created for your widget.
The best way to handle the templates is to learn from their code. My personal suggestion is to copy over the templates to the current theme and work directly on them, doing one change at a time. The templates require learning curve to use and there’s no single answer to everyone’s problem.
Coming up next
Latest posts by RafaĆ Gicgier (see all)
- WP Doin Recognized among Top Web Developers in Poland - 29 October 2019
- WordPress application development ACF & Gravity Forms - 3 August 2019
- OptiPic.io – How and why to optimize the image on the site? - 8 April 2019