Use case: Custom featured content gallery

Notice: this article is written for advanced WordPress users, and assumes some prior knowledge. Specifically, you should know how to do the following things: Understanding and modifying PHP-code in WordPress templates, creating and using dynamic sidebars in functions.php, and working with widgets.

Another note: there are plugins that take care of featured content galleries automatically, but they don’t offer the kind of fine-grained control over the HTML that we like to have. This is a professional scenario.

Say, the customer demands a top-of-the-line, bleeding edge, fancy pantsy featured content gallery on his homepage. For this, you have been given the latest and greatest Javascript and HTML5 driven gallery. It has everything: smooth animations, sound effects, the works. The only thing it doesn’t have is a ready-made plugin that embeds the gallery in WordPress for you, because it’s custom built for this customer only. The customer also wants to be able to show any content from the site in the gallery.

Oh, and he wants it done yesterday.

Examining the static HTML, you notice that the repeating element that holds the content looks something like this:

As there is no standard way to render that kind of HTML in WordPress, the only way to solve this is to write custom code that fetches articles and pages based on, for example, a custom field, and render them in a loop.

You can do it faster and easier with Dynamic Content Widget.

Step 1: Create subtemplate

Create a file called ‘gallery-item.php’ in your theme directory.

Copy and paste the HTML of the repeating element into this file, replacing the static elements with the dynamic ones in the subtemplate. Here’s a hypothetical example: