WordPress.org

Forums

[resolved] Get Tag/Category name from the slug (5 posts)

  1. David Gard
    Member
    Posted 5 years ago #

    Hey all,

    I am pulling the name of the selected Tag or Category out of my query string using $_GET['tag']/$_GET['cat']. I'm wondering if there is anyway of getting the Tag/Category name from just the slug, so that I can output it on the page?

    Thanks.

  2. MichaelH
    Member
    Posted 5 years ago #

    See Function_Reference/get_term_by

    $cat_slug = 'my-slug'
    get_term_by('slug',$cat_slug,'category');
  3. David Gard
    Member
    Posted 5 years ago #

    Hi Michael,

    May be a stupid question, but how do you out put that data? When I try to assign get_term_by('slug',$cat_slug,'category'); to a variable or echo it, as below, I get 'Catachable fatal error: Object of class stdClass could not be converted to string'

    $term = get_term_by('slug',$cat_slug,'category');
    echo get_term_by('slug',$cat_slug,'category');

    When just leaving as you put above nothing is output on the screen.

    Thanks.

  4. David Gard
    Member
    Posted 5 years ago #

    Ok, putting it in an Array will capture the data, but I still cannot output it.

    $term = array(get_term_by('id',$slug_ID,'category'));
    print_r($term); // Works, but don't need all the data.
    print_r($term->name); // Does not disply anything. Name is listed in the above output.
    echo $term->name // Also does not work

    Any help appriciated.

    Thanks.

  5. David Gard
    Member
    Posted 5 years ago #

    Got help from a friend, sorted.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.