paolodit
Member
Posted 3 years ago #
Hi
I am doing a dirty hack, and all I want is the category title to be passed to php on the single.php template.. Simple right?
http://codex.wordpress.org/Template_Tags/single_cat_title
So at top of single.php, I do:
<?php
$current_category = single_cat_title("", false);
echo "[" . $current_category . "]";
?>
And it doesn't work. Nor do any of the other ideas I had. help?!
thanks
leogermani
Member
Posted 3 years ago #
This will only work inside the Loop
paolodit
Member
Posted 3 years ago #
Fanx for the pointer.... But what's the workaround?
Should I write my own function, that extracts the title from the open socket within the page, strips out text, and works out where the title is?!
Surely there is an easy peasy way?
thanks again
leogermani
Member
Posted 3 years ago #
ok,
sou you want a ready code...
you are lucky today:
global $wp_query;
$curcat = $wp_query->get_queried_object();
$curcat = $curcat->cat_ID;
Then use this ID to get what you wanto from the category. The name, link.. etc.. with functions like:
get_cat_name
get_category_link
etc...
cheers
Leo,,
paolodit
Member
Posted 3 years ago #
Dude
It IS my lucky day. That is most appreciated. You are a legend.
THANK YOU
paolodit
Member
Posted 3 years ago #
Ah no... I spoke to soon!
thanks for trying.. Appreciated
Maybe your solution only works on the category page, not within the single.php theme template?
paolodit
Member
Posted 3 years ago #
I will paypal you a beer, if you can solve. thanks!
leogermani
Member
Posted 3 years ago #
cmon
global $wp_query;
$curObj = $wp_query->get_queried_object();
$curObj = $curObj->ID; //or maybe post_ID .. post_id
$categories = array();
foreach(wp_get_post_categories($curObj) as $cat_id) {
$categories[] = get_cat_name($cat_id);
}
leogermani
Member
Posted 3 years ago #
ps - .. Im not testing this ok.. it might not be exactly it, but this is the way...
Leo,,
paolodit
Member
Posted 3 years ago #
Much thanks.. that works like a baby.
my email address is at a well known gmail email provider, with paolodit as my name... Let me know your paypal address by email.
thanks!