WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a single page that displays one and only one category of the post that is showing. However the post can have more than one category.

If the user arrives from a category page I want to show that category name, and a random one otherwise.

Which is the quickest way to achieve this?

share|improve this question

closed as too broad by tfrommen, kaiser, s_ha_dum, gmazzap, Wyck Oct 28 '13 at 6:22

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

It would be great if you share the codes you already have tried on!! – Maruti Mohanty Oct 23 '13 at 11:27

Either use http referer or pass own param with your urls determining referring category. The first option seems better to me.


//check, whether a user does not access a page directly
    //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
        echo get_random_category_function();
    echo get_random_category_function();

function get_random_category_function() {
    //code for generating random category
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.