The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End WordPress: show title of the term (custom taxonomy) inside the custom post type

  • This topic is empty.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
  • #153551

    Hi all,

    I created a custom post type “Products”, and created also categories for that custom post type (so using custom taxonomies). These categories are 5: “Products” (main), and its children, “first”, “second”, “thirth”, “fourth” (their template is managed by “archive.php”).

    When I am in the page of the single category (which shows the list of all the posts of that category), for instance “second”, everything is ok: I can show the category title with this code:

    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
    echo $term->name;

    But when I am in the single page of a post of that category (managed by the template “single-products.php”), for instance “Prodotto 1” of the “second” category, the code doesn’t work. I need to show somewhere in this page that “Product 1” is in the category “second”.

    Thanks a lot!


    What if a product is attached to more than one category?

    Either way, try this perhaps:

    $custom_taxonomy = get_the_terms(0, 'category');
    if ($custom_taxonomy) {
        foreach ($custom_taxonomy as $custom_tax) {
            echo $custom_tax->slug;

    Replace ‘category’ (in the first line) with the actual name of your custom taxonomy.


    Hi Senff,

    fortunately a product cannot be in 2 places…
    Anyway, like the code I used before, this outputs nothing ;((((

    It’s like, when inside the single product, every info about his subcategory is lost… I cannot figure why!

    Tomasz Lisiecki

    Hello :)

    There you go

    $name = wp_get_post_terms(get_queried_object()->ID,'projects',array("fields" => "names"));
    echo $name[0];


    wp_get_post_terms returns an array of all assigned categories to that post, hence $name[0]. It is also future proof, so if you want to display all categories of the post at some point, you can just loop through $name variable.

    Hope it helps ;)


    Thanks Tomasz,

    I think I should replace “projects” (your code) with “products”?

    Anyway, it throws an error, because after that piece of code the rest of the site disppears, and the code outputs nothing…



    There must be something wrong in the “echo…” line, because removing that, the page doesn’t break, although it outputs nothing…

    Tomasz Lisiecki

    I think I should replace “projects” (your code) with “products”?

    Yes, you should. Sorry.

    What is your PHP version?

    The get_queried_object()->ID may not work if it’s old, as older PHP versions didn’t support accessing objects data like that. I think it is supported since 5.4=<

    Anyway. All you need to do is pass a post ID as a first parameter. Your own choice how you do it :) This was my way.


    Hi Tomasz, I am using MAMP and PHP 5.4.1!
    I’ll give other tries, then, thanks a lot for your help anyway!

Viewing 8 posts - 1 through 8 (of 8 total)
  • The forum ‘Back End’ is closed to new topics and replies.