LearnDash Design & Tutorials

LearnDash Design & Tutorials

LearnDash Course Grid Returns No Results? Show a Custom Message.

Affiliate Disclosure: We may be compensated if you use our links to make a purchase. We are extremely selective in who we partner with & only recommend products we believe in. Our affiliate relationships do not influence our recommendations.

When using LearnDash course grid parameters to customize the output of the grid, some users or instances might return no results. By default, when the course grid doesn’t show any courses, the area is left blank. But what if you want to display a custom message in place of your grid when no courses are available?

This article will show you how to do that.

Bonus Tip: We’ll even show you how to display a custom Elementor template for a course grid with no results. šŸ¤Æ

Using a Custom Code Snippet

First, let’s quickly talk about the code we’ll be using to make this happen. It’s PHP code that uses a LearnDash hook.

My preferred way to make changes like this is with theĀ Code Snippets plugin. You could also add this code to theĀ functions.php file of your child theme (if you’re using one), but I find Code Snippets to be easier to manage and less likely to bring your site down if you make a mistake.

  1. Install & activate “Code Snippets” from your WordPress plugins page
  2. Navigate toĀ Snippets > Add New in the left sidebar
  3. Give your snippet a name. Something like “LearnDash Course Grid – No Results Message”
  4. Paste the code snippet into theĀ Code area
  5. Choose theĀ Only run on site front-end option
  6. Click theĀ Save Changes and Activate button

The Code

Here’s the code you’re going to use. It uses a filter for the ld_course_list hook. First, it checks to see if any courses have been found in your course grid query. If it doesn’t find any, it updates the $output parameter to include a custom message.

/**
 * Example usage for ld_course_list filter. Display a custom message when the shortcode returns no results
 */
add_filter(
	'ld_course_list',
	function( $output, $atts, $filter ) {
		// If the ld-course-list-items div does not have items/courses to display, use a custom message as output.
		if ( preg_match( '@<div class="ld-course-list-items row">\s+</div>@', $output ) ) {
			$output = "<p>No courses found.</p>";
		}
		// Always return $output.
		return $output;
	},
	10,
	3
);

What you can customize

In the code above, the only part that you should touch is the part following $output =, inside the quotes. This is what will be displayed when no courses are found.

The example above will display, “No courses found.”

This code uses HTML, thus why you see the opening <p> and closing </p> tags. That just stands for “paragraph,” and it ensures your text inside of it will be styled properly.

If you know HTML, feel free to get creative here. Use multiple <div>s, write your own custom CSS, etc.

Use an Elementor Template

If you don’t know HTML but still want to do something creative or more elaborate in this space, you can use an Elementor template.

  1. Create a new Elementor Section and save it in your templates
  2. Go to Templates > Saved Templates
  3. Copy the Elementor Shortcode value

Elementor saved template shortcode

  1. Replace the $output = line above with this, using your unique template ID

$output = apply_shortcodes( '[elementor-template id="2154"]' );

Now when no courses are available in your grid, your custom Elementor template will be used.


Please share in the comments if you create something fun šŸ˜€.

Previous

Design Upgrade Compatibility with the Elementor for LearnDash Add-On

Next

How to Change the “Leave a Comment” Title in Focus Mode

4 Comments

  1. Jon

    Hello!

    Thanks for putting this out there however both versions of code did not work for me. I tried an Elementor Template and just the text version and nothing came back, just empty space. I don’t know how to code, but could it have something to do with the shortcode fields? (e.g. [ld_course_list progress_bar=”true”])

    Thanks for your help!

    • Hi Jon. Did you use the code in the purple box? Where did you put this code? It needs to be added as a new snippet in the Code Snippets plugin. Is that where you put it?

      That shortcode is structured properly so it’s not an issue with that. Although, that shortcode doesn’t limit which courses are shown, so it will show all of your coursesā€”which means, as long as you have 1 course, the “no results” message will not be shown (because there are results).

  2. Jon

    Hello again,

    Sorry I made a mistake! I was using Code Snippets but it was in safe mode, hence not executing the code. After trying again, both variations of the code worked perfectly. It’s a really handy piece of customisation! Thank you!

  3. Very cool and also very useful. It would be neat to create a form within the Elementor template that prompts the user to “submit a course request” to collect feedback on what visitors are looking for, along with a list of categories/tags to guide them to the existing courses, as opposed to a blank no results page.

Leave a Reply

Your email address will not be published. Required fields are marked *

Powered by WordPress & Theme by Anders Norén