Forum Moderators: rogerd & travelin cat

Message Too Old, No Replies

Create redirection to random post system

         

vmackey1

5:53 am on May 9, 2020 (gmt 0)

5+ Year Member



Hello,

I hope everyone is safe.

Ok, so I need a little advice.
Basically, I have a blog in which I would like a button that will lead to random posts of my WP website. But I also would like to give the choice to my visitors to filter for what type of posts (by the category type) they would get random posts.

For example:

Let’s say that my blog have 5 categories of posts: dog, cat, bird, fish, wolf.

We will have the “Random” button, and next to it, a choice by checkbox for each of these 5 categories. If the user check the box for “dog” and “cat” only, after clicking the “Random” button, a random post only from the “dog” or “cat” category will be loaded. Not “bird”, “fish” or “wolf”.

Currently, I temporarily set 5 random buttons, one for each category. If I click on the one associated with the “dog” category for example, a random post from the “dog” category will appear. So of course, I would like to replace that system with one button only, and the 5 checkbox.
Preferably, I would like the script to FIRST randomly choose one of the category for which the checkbox have been checked. And SECONDLY, to randomly choose a post for this category.

Here is the code I have for the 5 buttons.
Can you help me modify it to replace it with the checkbox system and one “random” button?

(NOTE: I am using the plugin “Redirect URL to Post” to generate the url redirecting to a random post)

[su_button url="https://example.com/?redirect_to=random&cat=2" style="flat" background="#2D9600" size="20" center="no" rel="nofollow"]DOG [/su_button]
<br>

[su_button url="https://example.com/?redirect_to=random&cat=5" style="flat" background="#2D9600" size="20" center="no" rel="nofollow"]CAT [/su_button]
<br />

[su_button url="https://example.com/?redirect_to=random&cat=6" style="flat" background="#2D9600" size="20" center="no" rel="nofollow"]FISH[/su_button]
<br />

[su_button url="https://example.com/?redirect_to=random&cat=7" style="flat" background="#2D9600" size="20" center="no" rel="nofollow"]BIRD [/su_button]
<br />

[su_button url="https://example.com/?redirect_to=random&cat=8" style="flat" background="#2D9600" size="20" center="no" rel="nofollow"]WOLF [/su_button]
<br />


I hope my explanation was clear.

Take care.

[edited by: not2easy at 10:39 am (utc) on May 9, 2020]
[edit reason] exemplified domain [/edit]

not2easy

11:08 am on May 9, 2020 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Hello vmackey1 and welcome to WebmasterWorld [webmasterworld.com]

Sorry for the edits, but the "example.com" domain only exists to use for examples while someone owns that other domain.

You might need to make a stack of widgets that would be called with a custom javascript because the plugin works via server-side PHP and the radio button choices would require a reload to offer only those choices. For example if the radio buttons chosen were CAT and WOLF then a different menu list of options would be required to load. I personally don't see that as an improvement to offering the categories as a list. If the selections are shown in a sidebar widget for example, then the visitor can view one and then another. The user experience is important and they can't view more than one result at a time anyway. ;)

vmackey1

12:05 pm on May 9, 2020 (gmt 0)

5+ Year Member



Thank your reply and for editing the urls.

So basically, design-wise, I will have no choice but to go for the "1 button + 5 checkbox" display instead of the "5 buttons" one. That is why I am looking to edit all of that.

Regarding your solution, I thought that editing the example code given would be enough to implement the checkbox and associate these to the randomize system (I only have very basic knowledge in development.) A simpler tweak wouldn't be possible here?

not2easy

12:34 pm on May 9, 2020 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Selecting options via radio button or checkboxes includes a 'submit' button to process the choices, generally that needs to be processed using a separate script. I suggested javascript because it needs to be client side for visitors to make selections. PHP is processed before the page loads so whatever exists in the PHP script is processed before anyone sees the page where the results would be. That's why there would be another page load before a person could get the menu with the chosen selections - and then they would need to choose again.

Each of your buttons would choose a random post from the category the visitor selects. They can't click two buttons so I am missing the point of offering a different selection of buttons after they choose two (or more) categories. That would just make them choose twice. You still only get one random post per any button. Have you thought of using Tags rather than Categories? That would offer more options per choice (usually). The plugin is offering a way to filter your site search but it offers various ways to filter.

I imagine that an extra customization might be possible within the plugin, but for that you would be asking at the plugin's help/support site because we don't customize plugins here.