Support » Fixing WordPress » need PHP help for get_the_category

  • Resolved moshu


    I understand how to use this template tag as shown here:
    If I use it twice with that [0] changed to something else (like 1 or 2) it will give me the other category names. Fine till here.

    Now, I was wondering if I would like to put dynamically a link (anchor) around the last part:
    $cat->cat_name so that it will become actually a link to that category… what code do I need? I can do it “hardcoded” but I would like to use something like the bloginfo(url)/cat_name – is it possible?

Viewing 9 replies - 1 through 9 (of 9 total)
  • I get the feeling I’m not understanding your question. But I’ll take a stab anyway.

    Is this what you’re trying to do? (with extra spacing for clarity)

    <a href="<?php bloginfo('url'); ?>/category/<?php echo $cat->category_nicename; ?>">
    <?php echo $cat->cat_name; ?>

    Or is that what you meant by doing it “hardcoded?”

    I’ll try again…
    Here is the code in the Codex:

    $cat = get_the_category(); $cat = $cat[0]; echo $cat->cat_name;

    If a post is in more than one cat – it will display only the first one and as NOT a link (that’s the nature of this template tag)

    Now, if I use the same tag twice in the Loop, once with [0] and the second time with [1] it will display
    1. the first cat
    2. the other cat the post is in.

    What I want/would like: to make the displayed category names (in both cases) a link to the category (archive), as the_category would do.

    Is it clearer?

    Thanks for trying 🙂

    Something like this, where $which is an integer for the category you want?

    function one_category($which=0){
      $cat = get_the_category();
      $cat = $cat[ $which ];
      $link = '<a href="' . get_bloginfo('url') . '/category/' . $cat->category_nicename . '">';
      $link .= $cat->cat_name;
      $link .= '</a>';
      echo $link;
    one_category( 0 ); // a link to the first cat
    one_category( 1 ); // a link to the second cat

    If I’m still misunderstanding, then I’ll let somebody else have a try.

    $cat = get_the_category(); $cat = $cat[0];
    <a href="<?php echo get_category_link($cat);?>"><?php echo $cat->cat_name; ?></a>


    mdi, it’s posts like yours that remind me that even though I know a bit of PHP, I sure don’t know all the functions available in WP. I’m feeling a bit daft right now.

    But note that get_category_link() needs the cat ID as an argument, so that portion of your code should say

    <?php echo get_category_link($cat->cat_ID);?>

    Not exactly but it helped me a lot! Thanks.

    So, here is what I ended up:
    1st instance:

    $cat = get_the_category(); $cat = $cat[0]; echo '<a href="' . get_bloginfo('url') . '/category/' . $cat->category_nicename . '">';
    echo $cat->cat_name;
    echo  '</a>';

    …other stuff here…
    2nd instance:

    $cat = get_the_category(); $cat = $cat[1]; echo '<a href="' . get_bloginfo('url') . '/category/' . $cat->category_nicename . '">';
    echo $cat->cat_name;
    echo  '</a>';

    It can be used for example to display one category before the post content and another cat after the content.

    Thanks again, Adam.

    no probalo

    I just noticed the code posted by mdi – thanks!
    That’s simpler and nicer than mine and it does the same 🙂

    The code below worked for me too:

    <?php echo get_category_link($cat=#);?> (# is the cat number)

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘need PHP help for get_the_category’ is closed to new replies.