0

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?

1
  • 1
    It would be great if you share the codes you already have tried on!! Oct 23, 2013 at 11:27

1 Answer 1

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
}

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