Forums

[resolved] Get Category URL (8 posts)

  1. oldnumber9
    Member
    Posted 2 years ago #

    I am trying to generate an image link for the category of each post. Here is what I have so far:

    <a href="**NEED CAT URL HERE**">
        <img src="<?php bloginfo('template_url'); ?>/images/<?php foreach((get_the_category()) as $category) {echo $category->cat_name . '-'; } ?>tag.png"></a>

    As you can see I got the individual pix to load for each category, but I cant find a way to load the URL. Any help??

    Here is the site: http://www.oldnumbernine.com/mcjh/

  2. Mark / t31os
    Moderator
    Posted 2 years ago #

    The code you have is only going to render one image, do you realise that?

    You're outputting a loop of categories inside the src part of the image..

    So you'll end up with one image that has a path like...

    wp-content/themes/YOURTHEME/images/category1-category2-category3-category-4

    and so on....

    Gimme 5 mins, i'll write you something... :)

    EDIT::
    Ah, i think i misread slightly, you're doing this inside the loop for each post...

    I'll see if i can make you something anyway.. ;)

  3. Mark / t31os
    Moderator
    Posted 2 years ago #

    One quick question, do you expect the result to return more then one category or do you want/expect it to return more then one?

    I'll post some code in a moment, we can always adjust it if it's not quite suited as is... :)

  4. Mark / t31os
    Moderator
    Posted 2 years ago #

    Just wanted to test a few different ideas to get it right, here you go...

    <?php
    $the_cat = get_the_category();
    $category_name = $the_cat[0]->cat_name;
    $category_description = $the_cat[0]->category_description;
    $category_link = get_category_link( $the_cat[0]->cat_ID );
    ?>
    
    <a href="<?php echo $category_link; ?>" class="category_logo">
    <img title="<?php echo $category_description; ?>" src="<?php bloginfo('template_url'); ?>/images/<?php echo $category_name; ?>-tag.png" alt="<?php echo $category_name; ?>" />
    </a>

    Pretty sure (by looking at your site) you are expecting one category for the result...

    Hope that helps... ;)

    NOTE: Added a class to the link so you can style it with the CSS to remove the border or anything..

    Example:
    .category_logo {}
    and
    .category_logo img {}

  5. oldnumber9
    Member
    Posted 2 years ago #

    You are right about the loop. What I have now is an icon that changes depending on the category the img src returns. There will only be one category assigned to each post, so I need the icon to link to that category. Does that make any since?

    Thanks for you help!

  6. Mark / t31os
    Moderator
    Posted 2 years ago #

    Above code will work as is, and will only return the first category...

    It will link as required to... :)

    Any issues, check the path for the image being generated is correct...

    I can help further if required.. ;)

  7. oldnumber9
    Member
    Posted 2 years ago #

    Success!! Thanks so much for your help!

  8. Mark / t31os
    Moderator
    Posted 2 years ago #

    You're welcome .. ;)

    Created just for you.. ;)

Topic Closed

This topic has been closed to new replies.

About this Topic