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?

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

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    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

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