Home › Forums › Back End › WordPress: show title of the term (custom taxonomy) inside the custom post type
- This topic is empty.
-
AuthorPosts
-
October 20, 2013 at 8:45 am #153551
mattiafrigeri
ParticipantHi 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!
October 20, 2013 at 5:19 pm #153573Senff
ParticipantWhat 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.
October 20, 2013 at 11:22 pm #153583mattiafrigeri
ParticipantHi 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!
October 22, 2013 at 3:00 am #153671Tomasz Lisiecki
ParticipantHello :)
There you go
$name = wp_get_post_terms(get_queried_object()->ID,'projects',array("fields" => "names")); echo $name[0];
Note:
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 ;)
October 22, 2013 at 11:43 pm #153787mattiafrigeri
ParticipantThanks 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…
;(
October 22, 2013 at 11:50 pm #153789mattiafrigeri
ParticipantThere must be something wrong in the “echo…” line, because removing that, the page doesn’t break, although it outputs nothing…
October 23, 2013 at 12:38 am #153793Tomasz Lisiecki
ParticipantI 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.
October 25, 2013 at 8:03 am #154028mattiafrigeri
ParticipantHi Tomasz, I am using MAMP and PHP 5.4.1!
I’ll give other tries, then, thanks a lot for your help anyway! -
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.