WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] need PHP help for get_the_category (10 posts)

  1. moshu
    Member
    Posted 6 years ago #

    I understand how to use this template tag as shown here:
    http://codex.wordpress.org/Template_Tags/get_the_category#Show_the_First_Category_Name_Only
    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?
    Thanks!

  2. Adam Brown
    Member
    Posted 6 years ago #

    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; ?>
    
    </a>

    Or is that what you meant by doing it "hardcoded?"

  3. moshu
    Member
    Posted 6 years ago #

    I'll try again...
    Here is the code in the Codex:

    <?php
    $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 :)

  4. Adam Brown
    Member
    Posted 6 years ago #

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

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

  5. mdi
    Member
    Posted 6 years ago #

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

    ...???...

  6. Adam Brown
    Member
    Posted 6 years ago #

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

  7. moshu
    Member
    Posted 6 years ago #

    Not exactly but it helped me a lot! Thanks.

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

    <?php
    $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:

    <?php
    $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.

  8. Adam Brown
    Member
    Posted 6 years ago #

    no probalo

  9. moshu
    Member
    Posted 6 years ago #

    Oops...
    I just noticed the code posted by mdi - thanks!
    That's simpler and nicer than mine and it does the same :)

  10. dreamyguy
    Member
    Posted 6 years ago #

    The code below worked for me too:

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

Topic Closed

This topic has been closed to new replies.

About this Topic