Either use http referer or pass own param with your urls determining referring category. The first option seems better to me.
The
//check, whether a user does not access a page directly
if(isset($_SERVER['HTTP_REFERER']))
{
//get category object
$category = get_category_by_path($_SERVER['HTTP_REFERER'], false);
//if category URL was passed
if(NULL !== $category)
echo $category->name;
//otherwise get your random category
else
echo get_random_category_function();
}
else
echo get_random_category_function();
function get_random_category_function() {
//code for generating random category
}
Related Posts:
- How do I get the category URL from get_the_category?
- Create single.php for specific category by category id
- How to get category id in single.php wordpress?
- in_category() works in single.php but not in page.php?
- previous/next_post_link in the same sub-category?
- Send to different single pages by category when multiple categories exist
- How to make the link to the category for a post go to corresponding page number in archive?
- Single.php Category Post Count
- Problems with my conditionals in single.php by category
- Get next post in same category not working WP 4.7.4
- Simple way to style posts of a single category differently in the loop and in single
- How to get nav to show current_page_parent class when on regular post (not blog), differentiated by category
- Displaying links to all posts of the same category on the post page
- Ignore the Ad on Specific Categories
- custom wp_list_categories in single.php
- loop in single.php of the same category
- Display Some text in some specific category not all category
- When category name and title are the same … WP will select archive.php instead of single.php
- Using template file for single post in sub-directory multisite
- Code to display category-specific single.php is overriding normal single.php, too
- Display Referring Category on single post?
- Modify / Rewrite single template url structure
- How do I get a single page navigation depending on the previous page?
- Retrieve Product category ‘NAME’ by product category ID – WooCommerce?
- Activate Gutenberg in category-descriptions
- How to show subcategories in categories else show posts
- How would I increase the Author Role to be able to add a new category?
- How to remove the parentheses from the category widget
- Filter front page posts by category
- How to get the url to tag & category base set by the user?
- How to: 301 Redirect /category/ to /customname/
- Custom Query to search through categories
- Root level category, tag, author and archive pages to work
- Get sticky post from category?
- Display one latest post from multiple categories
- Archive pagination causing 404 with permalinks structure
- Get a list of Terms for a specific category
- Get a variable from object in array [closed]
- Remove Archive Headline and Archive Intro Text fields on category and tag archive pages in WordPress Admin with Genesis framework
- How Do I Merge Categories With phpMyAdmin
- Edit the markup of categories list
- Show category children, one level
- a-z list, categories and sub categories in loop
- Category slug $_SERVER[‘REQUEST_URI’];
- wp_dropdown_categories and custom taxonomy + custom post type
- Random taxonomy category list
- Linking products to categories
- Multiple Domain Names – One WP Install (non-Multisite) – Default Each Domain name to Product Category WooCommerce
- Show all posts for a specific category/author
- Resort get_categories
- How to call custom category field data in theme?
- Query Posts Exclude Entire Category
- One Specific Category To Permalink
- Hide woocommerce category name from specific pages
- WordPress category widget only show categories with children
- The arrangement of categories always change while posting article
- Woocommerce custom category page with pagination [closed]
- Menus don’t work after 4.4 [closed]
- Custom post and category template pagination problem
- How to list all categories in three divs dynamically based on the quatity of categories?
- Grouping categories by genre
- how to “not” display new posts with a specific category on the main page?
- Limit categories and it’s subcategories for specific group of users
- show all WordPress categories in a page
- Admin: Navigation Menus do not show empty categories/taxonomies in the list’s search tab
- getting post data in functions.php
- Category specific order
- Remove the number of posts displayed in wp_list_categories()
- Recent post by custom select query
- WP_safe_redirect not working
- Do not show sub categories in the loop on archive-product.php
- Having a template per category? Bad for server overhead?
- “Virtual category page” based on a custom field filter
- Anchor tag generates blank href attribute using echo esc_url( )
- Get categories of queried posts
- Limit Loop to One Category
- wordpress taxonomy query posts
- Different header for each category
- Subcategory is displayed before Parent Category
- sidebar troubles, taxonomy trouble
- Code for Category, Tags, Archive URLs
- Display all subcategories from parent category
- Custom Loop with certain number of one category
- “Special” category is displayed in all categories or show parent category on sub category
- How to show WooCommerce Categories on ‘shop’ page instead of products?
- How to list all subcategories from all categories but not from a certain category
- How to create Child Category page from scratch at wordpress?
- Automatically create tags and merge with categories
- How to insert category to post via php?
- How to list all categories a post is in, but exclude current post´s category on single post page
- Show Subcategory Name Instead of Parent Category
- Get category id of next / prev post inside the_post_navigation
- Limit Category/Tag Dropdown To blog_id in Multisite Install With Global Categories/Tags
- Custom built theme won’t filter categories
- WP_query exclude a category unless it has more than one catagory
- get IDs from links categories
- Narrow Down a Shop Page Results Based on a Product Tag in WooCommerce
- How to control order when displaying child categories
- Trying to WP_Query a category
- how do I exclude a category foreach