0

The Problem

I'm trying to display the current parent category title with the post title right under it. The problem is, I can't seem to get the posts current category inside or outside of the loop. I can get a list of parent categories. For instance, if I have a post in 3 different categories, and I browser to the single post in those categories, I want it to display the current category name at the top, not just the first [0] category it is in.


The Example

Test Post is in the categories: Cat1, Cat2

I go view the Test Post in Cat1 it should display Cat1 - Test Post

I go view the Test Post in Cat2 it should display Cat2 - Test Post


Trial and Error

get_the_category() returns both the categories: Cat1, Cat2

$post->post_parent always returns 0

echo $category[0]->name; will of course only return the top most category - Cat1

4
  • This is confusing. How do you view a post in the context of single.php from two different URLs?
    – Milo
    Commented Aug 23, 2013 at 16:57
  • You're right, my idea wouldn't work at all.
    – Howdy_McGee
    Commented Aug 23, 2013 at 17:04
  • How is this different from your other question? wordpress.stackexchange.com/questions/110319/…
    – vancoder
    Commented Aug 23, 2013 at 17:15
  • @vancoder They're similar but different. If you comments is correct though then yes, these questions come down to a core concept and wouldn't matter because of that. If that is true though, how do blog breadcrumbs work if 2 posts belong to the same category?
    – Howdy_McGee
    Commented Aug 23, 2013 at 17:39

1 Answer 1

4

The very broad answer for why this doesn't work is that the internet is stateless. Basically each request for a page is a separate and unique instance from other page requests.

Example

Let's say post-1 is in category-1 and category-2.

When Wordpress loads post-1 on single.php it has no way of knowing how the user arrived at the page. It only knows that both category-1 and category-2 are associated with the post.

It is not possible to get the "current category" because it is no longer in a category.

This behavior also creates confusion with breadcrumbs in Wordpress. A user's breadcrumb path might look like this.

Step 1. Home
Step 2. Home->category-1
Step 3. Home->post-1

The expected behavior is often for the last step to look like this.

Step 3. Home->category-1>post-1

But if that worked, then this would also work.

Step 3. Home->category-2->post-1

And that would be bad for search engines because now the same post has two different URLs.

However

If you really want to show the category that the user came from you could try something like this. Remember, the internet is stateless so you need a way to pass information from one page to another.

Category Template

On category.php you can alter your permalink to the single post and include a query string parameter in the form of ?category=id. That might look something like this.

<?php
$cat_obj = get_category( get_query_var( 'cat' ) );
$cat_id = ( $cat_obj ? $cat_id = $cat_obj->term_id : $cat_id = '' );
?>
<a href="<?php echo get_permalink()  . '?c=' . $cat_id ?>"><?php the_title(); ?></a>

Your permalink should now look something like this where ?c=19 is appended to the normal permalink.

http://www.example.com/post-name/?c=19

Single Template

On single.php you need to grab the category ID from the query string, get the name of the category and then display on the page. The code for that might look like this.

You'll want to check if the query string parameter is set. If it is, use esc_html() to sanitize it for security, then check to make sure it's numeric and not something else like "blah".

Once done with the validation, get the category name and output on the page.

if ( isset( $_GET['c'] ) ) {
    $cat_id = (integer) esc_html( $_GET['c'] );
    if ( is_numeric( $cat_id ) ) {
        $cat_obj = get_category($cat_id); 
        $cat_name = $cat_obj->name;
        echo '<span>' . $cat_name . '</span>';
    }
}

Caveats

If the query string parameter does not exist no category will be displayed. There are many ways that a user might arrive at a single post page and this only handles a very specific use-case.

This may also negatively impact SEO if the search engine perceives there to be multiple unique URLs to the exact same content.

2
  • Thanks, that makes sense. Not to seem unappreciative but if it negatively impacts SEO then I'll probably just find something more static to display. Wish I could upvote this more.
    – Howdy_McGee
    Commented Aug 23, 2013 at 20:35
  • 1
    That's perfectly fine. I've had to set archives, categories, and tags, to noindex, follow for some sites because Google complained of duplicate content.
    – epilektric
    Commented Aug 23, 2013 at 23:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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