• For category pages, I provide an rss link in the header, using the following code:

    if (is_category()) {
    $cat_obj = $wp_query->get_queried_object();
    $cat_slug = $cat_obj->category_nicename;
    ...
    }

    I would like to do this for tag pages as well. Apparently get_queried_object() does not apply to tag pages, so it looks like I’d have to take a completely different approach. If someone could point me to the solution, I’d sure appreciate it!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter ericr23

    (@ericr23)

    I appear to have solved it:

    if (is_tag()) {
    $tag_obj = get_query_var('tag_id');
    $tag_array = get_tag($tag_obj);
    $tag_slug = $tag_array->slug;
    ...
    }

    Apparently get_queried_object() does not apply to tag pages

    why not?
    try and use this to check it:
    <?php if( is_tag() ) print_r( $wp_query->get_queried_object() ); ?>

    or try and use:
    <?php $tag_slug = get_query_var('tag'); ?>

    Thread Starter ericr23

    (@ericr23)

    That’s an enticing shortcut. I tried get_query_var(‘tag’), but it returns the tag id, not the slug.

    You’re right, get_queried_object() on a tag page does return the tag info.

    I was able to combine both:

    if (is_category() || is_tag())
    {
    $cattag_obj = $wp_query->get_queried_object();
    $cattag_slug = $cattag_obj->slug;
    $cattag_tax = $cattag_obj->taxonomy;
    ...
    }

    That’s an enticing shortcut. I tried get_query_var(‘tag’), but it returns the tag id, not the slug.

    <?php $tag_slug = get_query_var('tag'); ?>

    this returns the tag slug in my test site;
    so it actually might depend on the permalink setting (?) as my permalink settings are set to /%postname%/

    Thread Starter ericr23

    (@ericr23)

    That’s odd. My permalinks are /%year%/%monthnum%/%day%/%postname%/ but that doesn’t apply to a tag page.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to retrieve tag slug in header’ is closed to new replies.