Tag Archives: carousel

How To Create a Related Posts Slider / Carousel in WordPress

This is a very common task: to show related content under a given post. Surprisingly there is no such built-in function in WordPress so here are two ways to achieve it.

The Easy Way: With The Free Shortcode Revolution Plugin

See in this video:

The plugin can be downloaded from the official repository.

The DIY Way: Code

If you are here to just get the work done, use the plugin suggested above. If you are looking to learn and / or need a custom solution, let’s see the code. It’s pretty simple.

First, let’s figure out what would related mean? You don’t have to create a complicated AI. The related posts can be selected from posts from the same category and/or with the same tags.

Let’s dive in the code. You will need to create a function – it could be a function right in your theme, which will then be used in the single-post.php template, or (probably better) you can do it with a shortcode. I’ll leave this decision for you.

global $post; // this is the currently shown post on the page		
if(empty($post)) return ''; // just make sure you don't run this in the wrong place where $post may not exist

// let's prepare the WPQuery parameters
$query = [];	

// We will add both tags and category as criteria. Feel free to use only one of them
$tags = get_the_tags($post->ID);
if(empty($tags) or !is_array($tags)) $tags = [];
$tags_arr = [];
// we can't use $tags directly as they come from get_the_tags() because they are objects. So we are filling the new $tags_arr
foreach($tags as $tag) $tags_arr[] = $tag->name;						
$query['tag_slug__in'] = $tags_arr;

// similar for categories
$cats = get_the_category($post->ID);
$cats_arr = [];
foreach($cats as $cat) $cats_arr[] = $cat->term_id;
$query['category__in'] = $cats_arr;

// Let's say we want 5 random posts. We'll select 6 and then strip to 5 just to make sure the current post is not included.
// You could use the "post__not_in" attribute for this but it has some specific which make us prefer to clean the unnecessary post in PHP.
$query['posts_per_page'] = 6;
$query['orderby'] = 'rand';
$wp_query = new WP_Query($query);
$posts = $wp_query->posts;

// here we'll cleanup the current post with array_filter and a closure
$posts = array_filter($posts, function($p) use($post->ID) {				
    return ($p->ID != $post_id);					
});

if(5 < count($posts)) array_pop($posts);

That’s it. You’ll have the related posts in the $posts variable. If you want to learn more how WP_Query works, check here.

Watch out for some pitfalls if you’ll use $posts outside of the loop. Or just use WP_Query -> have_posts().

From then on, you can use any JS slider and prepare the required JS variables.

In Shortcode Revolution we preferred to build a vanilla JS slider using most of the code from this great guide.

Alternatively you can use aready jQuery carousel plguin like Slick.

 

Shortcode Revolution for WordPress

During our work on various WordPress installations I have figured out that we repeatedly create (oh well, kind of copy/paste) layout elements, dynamic stuff like lightboxes and sliders, pieces of code in custom plugins, and so on.

Some of these codes slowly turned into a library so we decided to go ahead and package them into a new free plugin called Shortcode Revolution. (You can check it at our site or directly at the WordPress repo)

This is just a very first version so there is hopefully a lot more to come. But let’s have a brief look at what’s available:

Posts, Related Posts, and Comments

UI for posts / comments shortcode

This allows you to create widgets that contain posts related to a topic, searched by ID or tag, related to the currently shown post, and so on.

There are 3 handy layout modes: simple list, default (with a thumbnail and excerpt), and a carousel / slider.

And above this paragraph you see a slider / carousel of 3 posts related to this one.

Popup / Lightbox

modal box UI

This is such a frequent task, and this plugin really makes it easy. Just enter your contents, some clickable text and probably some custom CSS classes. Voila, the popup is created.

Click me!

Columns and Grids

Columns / Grids UI

I mean, it’s typical that the content on the web normally just stays in one column. But who said it should always be that?

Do you remember newsletters? I hope our readers are not all that young. Newsletters (the paper ones!) typically have content flowing in multuple columns. That’s not typical for the web not because it’s bad but because it was hard thing to do in the past.

Now with the new CSS techniques it’s not that hard. But you don’t always want to write CSS especially when you are writing a blog post, do you? This is where the columns shortcode comes handy!

The shortcode also allows you to create a grid. In the grid the content does not flow in the columns. Instead, each piece stays in its own grid cell. Like this:

Hey, I’m the first item

And I am number two

They can all be different

And can contain images (look right)

Chart from a personality quiz

This at left is from our quiz plugin WatuPRO

Third line, item 1

And number two

The ninth item goes here

Tabs

Tabs UI

Tabs allow you to place content within, well, tabs. What does it mean? Look at this:

As I told you, you canย  arrange content in tabs ๐Ÿ™‚ Just click on one of these tabs and you’ll see the content related to each one.
just a chartLet’s just have a chart here to look smart.

Buttons

Create buttons UI

There isn’t so much to explain here. You get an easy to use UI to create all kind of buttons.

Tables

create tables from CSV

This one is super simple – it creates table from an uploaded CSV file. Technically you can use a remove or a dynamic CSV as well – just replace the uploaded file URL with the remote one.

Tables aren’t that much fascinating – I am sure you have seem some! So no demo for this shortcode.

Flashcards / Flipcards

Create flashcard shortcodes

There cards have a front and a back side and reveal the back when the user clicks on them. Simple:

Here’s a Math task:

2 + 2*3?

Of course it’s 8 because multiplication has a higher priority than addition ๐Ÿ™‚

Who is she?

Adriana Lima

You can even combine shortcodes together like I did above. I placed two flashcard shortcodes in a grid so they can be side by side.

Data Shortcodes

creating a data shortcode

They are excellent for creating profile pages in communities, author pages, widgets containing some user data, and so on.

Custom Shortcodes

Just any content that you may want in many places on your site. Custom HTML, shortcodes, media, formulas, whatever. Easily repeatable and editable in one place.

Enjoy the plugin – it’s free and can save you many hours of work. Let us know your feedback, feature requests, and bug reports (yes, even bug reports!).