Build Your Own Business Website header image

Customize Next & Previous Posts Links – Part 6 – Add “Random Post” Link

Difficulty Level -

Filed Under Topics - ,

Listed Under Lesson Subjects - ,

Applies to -

Whoops, you've found some premium content!

Watch the opening clip of this video to preview it,
the full video is available to paid members.

This is the 6th in an 8 part series that shows how to customize the Next Post/Previous Post links in Thesis. Now we are going to add a random post link to the custom post navigation. To do this we will install the Random Post Link plugin and create a function to display the link.

Video Transcript

So then the next thing we’re going to do is we’re going to add this little random post link here to the middle of this. So that requires us to use a plugin.

So we’re going to come over to the dashboard and we’ll go to plugins, add new, and we’re going to search for Random Post plugin or Random Post link… I’m sorry. Okay and this is the one, Random Post link by Sribu and you can see it indicates that I already have it installed because I do actually already have it installed. But we’re going to go to the page anyway because I want you to see… here’s how it works. In order to get your random post link, you just enter this function in the middle of your… wherever you want to add it right? This function will result in a random post link. So we’re going to copy that and come back over to our custom functions php file and you know, we’re actually going to do this in a completely separate function. I was thinking I could incorporate it into this but the problem with that is I want to be able the check the logic and I don’t want this function to get too big. So in order to prevent it from getting too big, I’m going to use this.

So actually I’ve already written this function. I’ve got a function that says display random post link and then I’ve asked the question essentially to set the context, if it’s the homepage or if it’s an archive page or if it’s a single then I want to display this random post link. So if that’s the case then I’m going to display it. Otherwise if it’s a page for example, I’m not going to display it then if a function exists, random post link. Now what I’m doing here is I’m making sure the plugin is actually installed. If I just run this function without the page actually installed, it’ll crash because it’s calling a function that doesn’t exist. So anytime I use a function that is reliant on a plugin then I ask the question, if the function exists, this is the name of the function. Or if it’s a random post link is this random post link. Ask if it exists and then if it exists, I do the…I call the function. Otherwise, nothing happens.

So now I want to be able to link this… I want to be able to reference this somehow in CSS so I’ve decided to wrap it in a class which I’m calling byob random. So I’m echoing a div class byob random and then bringing in a link and then I’m echoing the link or I’m echoing the closing div tag. And we are adding that action to Thesis hook after content which is exactly the same hook this is hooked to. So Thesis hook after content display random post link, okay? Let’s go ahead and save this and let’s upload it and let’s test it.

Okay there you go, there’s our little random post link down here at the bottom. And if you click on this, it takes you to a different post, Vienteco Whole Banana. Let’s scroll down here…

0 Comments… add one
0 comments… add one

Leave a Comment