• Hi all,

    I was trying to implement this bit of code here which I borrowed from the following link.

    However when I tried to echo out the id, nothing came out. Any ideas?

    Here’s the code

    <?php
    $pageslug = $post->post_name;
    echo $pageslug;
    $category = get_category_by_slug('$pageslug');
    $the_id = '$category->cat_ID';
    echo $the_id;
    ?>

    Any ideas?

Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter jianchung

    (@jianchung)

    Sorry, the line $the_id = '$category->cat_ID'; should read $the_id = $category->cat_ID; ??

    Thread Starter jianchung

    (@jianchung)

    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.

    Thread Starter jianchung

    (@jianchung)

    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!

    Thread Starter jianchung

    (@jianchung)

    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;
    ?>
    Thread Starter jianchung

    (@jianchung)

    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 ??

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Getting category Id by slug’ is closed to new replies.