You can use wp_get_post_categories
to get the categories from your posts. Then you can use array_rand
to get one random category from that array.
Check out these links on how to use wp_get_post_categories
and array_rand
to suit your needs:
https://developer.wordpress.org/reference/functions/wp_get_post_categories/
https://www.php.net/manual/en/function.array-rand.php
Related Posts:
- How to know if get_posts() failed?
- Get category slug and display it on a query_post
- How to show next Post Thumbnail image in WordPress using current post id
- How to add paging to query
- Am I using get_posts wrong?
- Is there any need to use both wp_reset_postdata and wp_reset_query together?
- How to query the latest 5 posts and sort them by title?
- How to reset post data properly
- Custom Post Type Query issue
- I have WordPress setup on primary domain, how do I set it up on its subdomain using the same database?
- How to get url image page the right way?
- show posts in different styling
- WordPress Theme Preview Image
- What is a Theme textdomain?
- What are the advantages and disadvantages of Option Tree over the Customization API?
- Splitting WordPress theme CSS into multiple files, good or bad?
- Edit srcset and sizes attributes in Gutenberg image, cover and gallery – blocks
- do_shortcode() doesn’t do shortcodes ;)
- How to concatenate inside the _e() function the right way?
- How to sanitize select box values in post meta?
- Why to check if function doesn’t exists in functions.php?
- Pages: frontpage.php, home.php, posts page etc
- How to add a WYSIWYG text editor to the Category Edit Screen
- Different wordpress 404 template for different post type [duplicate]
- How to choose which template to be used for multiple taxonomy query?
- How to programmatically bring back “excerpts” field in post editor in WP 3.1+
- How to set dimensions of the post thumbnails (featured images)
- Fatal error: Call to undefined function get_header()
- Add a theme via symlink
- Alterntives to BEM syntax that comply with WordPress coding standards? [closed]
- How to determine which registered sidebar area a custom widget is loaded into
- How to optimize my query filtering out unwanted data?
- Integrating Html5Boilerplate’s Builder into a WP Theme
- Remove posts from query for events whose start date has passed
- Including wp-blog-header.php from functions.php remote call?
- Administration Pages Styling
- Theme settings keep getting reset/erased
- Custom button block doesn’t work
- How can i convert the figma design into wordpress [closed]
- A custom theme with support for multiple layouts
- WordPress Local Install Theme Folder Permission To Edit
- Display div only on the HOMEPAGE
- WordPress is adding margins and padding to my custom menu?
- What is the best way to organize template parts?
- Keep sticky posts out of query unless they have featured image
- wp_enqueue_script outside functions.php file?
- How can I make my options in an array and store theme in WP options one DB row?
- Differences between developing custom themes for wordpress.com and wordpress.org?
- How to control on which pages the Aldehyde theme’s main slider is shown?
- How to support letting users add their OWN logo to a custom theme?
- WordPress stylesheet isn’t being added
- How to detect and display a page only for IE?
- Does code in function.php differ from theme to theme
- How can I optimize this code? [closed]
- Issue on Getting Custom post type Thumbnail’s URL
- Forms won’t submit
- Change image size depending on page
- How do I provide for multiple crops of the same image in a theme?
- Why WordPress AJAX returns undefined however it works fine when I add static url instead of dynamic function?
- Twenty Seventeen Pages Loop
- What and where are the WordPress core-bundled scripts?
- How to add pages in wordpress using codes?
- How does the loop know which post to view?
- How to make a function occurs for one time?
- How to know if I am on 1st page
- Trying to link to a php template file but its blank
- A Reviews Page is Showing root Index.php instead of Template-Page
- Twentytwenty theme p-tags break em- and strong-tags that span several paragraphs
- Image cropping isn’t working correctly when using Offload Media
- Can any IDE (eg PHPStorm, VS Code) correctly show functions added via require get_template_directory() string results?
- Array for WordPress Avatar Size and Class
- custom js script is not loading
- Template for front page (latest posts)
- How to toggle WP Customizer Panel and Section with JavaScript API?
- is_active_sidebar() problem
- WordPress uploads do not show up and I see the white screen of death in some cases
- How to fix an issue with customizer live preview?
- Code for creating three vertical columns in WordPress Bootstrap theme
- Primary Menu Showing All Pages With No Sub-Nav
- WordPress 3.9 two menus in same position?
- Image load issue with custom page template on Internet Explorer
- Custom widget not appearing on front end
- Custom widgets in theme option page
- Adding custom widgets to theme sidebar on activation
- Showcase your wordpress themes [closed]
- wordpress page as website
- the_author() str_replace error
- How to make a multilingual wordpress site to be translated one-to-one without much effort and without using translate?
- Is there a way to have WordPress autodetect page templates in the page-templates directory and any sub-folders?
- Feeds are showing where Post archive page should be
- My wordpress site memory exhausted more than 1GB trying to debug with default
- Display first post on each page differently than the others
- Custom Theme functions.php – Using add_settings_field and input type=”file” to set custom logo
- Theme template file for a specific custom taxonomy
- How do I work with responsive images in WordPress? (img srcset/sizes)
- How can I use pre declared blocks in a php file?
- jQuery Snippet Not Working on WordPress Site [closed]
- WordPress Two Level Filters on Getting Custom Taxonomy Terms
- I want to resize post featured image without cropping image. How?
- Image and font support missing in editor when deploying theme remotely