Let’s say you are using Participants Database for a database of items, each with a unique serial number. You don’t want to show any of the records until a matching serial number is entered by the user.
So instead of showing this single result as a list of one item, how about showing that result as a “single record” display? This saves the user a click, taking them immediately to the information they are looking for.
To accomplish this, we need to use a custom template for the list shortcode. It turns out, it’s pretty simple because all we need to do is grab the ID of the record and use that to show a [pdb_single] shortcode. There’s more to it, of course, but that’s the core idea.
My approach here was to look at the default list template, then remove anything I didn’t need: mostly the loops that show the records in a table, and the pagination control. I need the search control, of course. Each of these three things (search, list and pagination) can be thought of as separate units, so it’s easy to just take them out. The container that wraps them all has to stay, though.
The spot occupied in the template by the list table is where our single record will go. We do that by invoking the shortcode using a WordPress function “do_shortcode()” which just expects the string that you would normally just put in the content. The advantage there is we can use variables to fill in some of the info.
First, we get the ID of the record we’re going to show. When the list template is active, the data it uses to display the records is in an array $this->records . We grab the ID of the first one in that list. If there isn’t one (for instance when the page is first loaded or if the search was for a non-existent number) the single record will take care of that situation. We just give it the ID we find like this:
<?php $id = is_array( $this->records ) ? key($this->records) : 0; ?>
<?php echo do_shortcode('[pdb_single id=' . $id . ']'); ?>
You can see how the $id value is inserted into the string that invokes the shortcode, telling it which record to show. The rest of the template is just putting in the wrappers and the search control.
The single record needs to be wrapped in that div with class “list-container” so that the AJAX search script knows where to put the result of the search: that is what it would expect to find if there was a list of results being displayed.
In a default setup, save this template in your theme directory in a folder named “templates”:
wp-content/themes/YOUR_THEME/templates/pdb-list-single.php For those who are using the Custom Template Folder add-on, put the template with the other templates you are using.
To use this template, you need to name it in the shortcode, and also add the “suppress” attribute so nothing gets displayed unless a matching search is performed:
[pdb_list search=true template="single" suppress=true]
If you need to change the text that is shown when no record is being shown, change the setting named “Record Not Found Error Message” under the Record Form tab.
One possible variation on this template would be to make it so that if there were multiple results, it would show a list, if there was only a single result, the single record would be shown using the single shortcode.