Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 t f, kaiser, s_ha_dum, G. M., 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.

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

1 Answer 1

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
}
share|improve this answer

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