WordPress.org

Ready to get started?Download WordPress

Forums

simple.. give php the category of current post - but it dont work! (10 posts)

  1. paolodit
    Member
    Posted 6 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

  2. leogermani
    Member
    Posted 6 years ago #

    This will only work inside the Loop

  3. paolodit
    Member
    Posted 6 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

  4. leogermani
    Member
    Posted 6 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,,

  5. paolodit
    Member
    Posted 6 years ago #

    Dude

    It IS my lucky day. That is most appreciated. You are a legend.

    THANK YOU

  6. paolodit
    Member
    Posted 6 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?

  7. paolodit
    Member
    Posted 6 years ago #

    I will paypal you a beer, if you can solve. thanks!

  8. leogermani
    Member
    Posted 6 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);
    }
  9. leogermani
    Member
    Posted 6 years ago #

    ps - .. Im not testing this ok.. it might not be exactly it, but this is the way...

    Leo,,

  10. paolodit
    Member
    Posted 6 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!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.