Skip Navigation

[Resolved] randomly displaying views

This support ticket is created 4 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Waqar 4 years, 11 months ago.

Assisted by: Waqar.

Author
Posts
#1403073

Tell us what you are trying to do?
create a custom taxonomy (tags) and display all of them randomly.. ie. changing on refresh.

i have a custom post type called "social media posts" and in that a custom taxonomy (tags) called "sm hashtags" .. I need to randomly display all of the tags for the "social media posts" post type on a page, ie. each time the page is refreshed the tags are in a different order.

I cant seem to figure this out ..

is it possible?

Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site?

#1403945

Hi Jen,

Thank you for contacting us and I'd be happy to assist.

The "wpv-post-taxonomy" can be used to show the attached taxonomy terms, but it can only show them ordered by name/title.
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-taxonomy

To show them in a random order, you'll need a custom shortcode that can show these terms, using the "get_the_terms" function:
https://developer.wordpress.org/reference/functions/get_the_terms/

Example:


add_shortcode( 'get_terms_random_order', 'get_terms_random_order_func');
function get_terms_random_order_func($atts) {
	$a = shortcode_atts( array(
		'type' => ''
	), $atts );

	$terms = get_the_terms( get_the_ID(), $a['type'] );

	shuffle( $terms );
	
	ob_start();

	foreach ( $terms as $term ) {
		echo $term->name.', ';
	}

	return ob_get_clean();
}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through active theme's "functions.php" file.

After that, you'll be able to use this new shortcode on your single post pages, like this:


[get_terms_random_order type="slug-of-taxonomy"]

Note: You'll replace "slug-of-taxonomy" with the actual slug of your custom taxonomy "sm hashtags".

I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1404879

I did everything above, tested the code in the toolset editor and it said it was ran with no errors. but when i paste the short-code into a post it does not return any data.

the slug of my shortocde is sm-hashtag, so I am doing [get_terms_random_order type="sm-hashtag"].

I am placing this on a PAGE ... will this not work on a page ?

Can I use this shortcode in a view?

#1405871

Thanks for writing back.

It is strange that the shortcode works on my test website, but not on yours. It should work on a single post/page and inside a view, alike.

Can you please share temporary admin login details, along with the link to a page where you tested this shortcode?

Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.