Sorry, the line $the_id = '$category->cat_ID'; should read $the_id = $category->cat_ID; π
alchymyth, tried that but it didn’t work as well…. π thanks for the help
This won’t work unless you have a category matching the name of that post. The function get_category_by_slug(); is referring to the slug of the category. The code above is assuming you want the information by inputting the post’s ID, get_the_category() is a better choice:
$category = get_the_category($post->ID);
try to chnage this line:
$category = get_category_by_slug('$pageslug');
to:
$category = get_category_by_slug($pageslug);
(you are using a string variable, but had that surrounded with quotes)
also, just for testing, you could try to add
var_dump($category);
after the line; that gives you a full output of the category object.
remove the line if al works ok.
jlevan: I was created categories based on the pages so they match. The intention was to pull the category based on the page slug, then pull the id and output it into a function π
alchymyth: I removed the quotes but still it did not work. Added the var_dump($category) and I got bool(false) as an output haha! Nice try!
I tried the var_dump($category) and got a whole bunch of gibberish π Any idea what this means?
object(stdClass)#314 (15) { [“term_id”]=> &string(2) “28” [“name”]=> &string(42) “Kuchai Lama Goon Wah XO Fish Head Mee Hoon” [“slug”]=> &string(42) “kuchai-lama-goon-wah-xo-fish-head-mee-hoon” [“term_group”]=> string(1) “0” [“term_taxonomy_id”]=> string(2) “28” [“taxonomy”]=> string(8) “category” [“description”]=> &string(0) “” [“parent”]=> &string(1) “0” [“count”]=> &string(1) “3” [“cat_ID”]=> &string(2) “28” [“category_count”]=> &string(1) “3” [“category_description”]=> &string(0) “” [“cat_name”]=> &string(42) “Kuchai Lama Goon Wah XO Fish Head Mee Hoon” [“category_nicename”]=> &string(42) “kuchai-lama-goon-wah-xo-fish-head-mee-hoon” [“category_parent”]=> &string(1) “0” }
if you get all these output, it means that it should be working;
for instance: this:
["term_id"]=> &string(2) "28"
means that the category id is 28;
this one means the same:
["cat_ID"]=> &string(2) "28"
(so actually, your $the_id = $category->cat_ID; could have worked.)
this:
["name"]=> &string(42) "Kuchai Lama Goon Wah XO Fish Head Mee Hoon"
is the name of the category;
which should be the same as the page title;
this:
["slug"]=> &string(42) "kuchai-lama-goon-wah-xo-fish-head-mee-hoon" is the slug of the category;
which should be the same as the page slug.
is it working now?
what is the output if you remove the var_dump code again?
the code will only work if the page name (i.e. the page slug, for instance ‘kuchai-lama-goon-wah-xo-fish-head-mee-hoon’ ) is identical to the category slug.
<?php
$pageslug = $post->post_name;
echo $pageslug;
$category = get_category_by_slug($pageslug);
$the_id = $category->term_id;
echo $the_id;
?>
I read around and got and idea. The values output are strings but not integer, so I tried using $the_id = (int)$category->term_id;
It didnt work but instead returned a 0 value after i echoed it. Later on when I removed the (int) term, the code worked perfectly!
Wierd! Case closed π