• Resolved lola rennt

    (@laurenserge)


    Dear WordPressers,

    I have a pretty stupid question, that I do need to ask due to my inability to correctly write php.

    I have this code that I use for an archive page.
    <?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?>
    I want to display the category for each post. The only problem now is that it just displays the category, but I want it to be a link to the category archive page. I tried many things, but I didn’t work. I know it is a probably a matter of brackets and spaces… but I did not manage to find out.
    Thank you very much for your help.

    x Lauren

Viewing 4 replies - 1 through 4 (of 4 total)
  • Why not just use the_category?

    But just in case you don’t like that:

    echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';

    Thread Starter lola rennt

    (@laurenserge)

    <?php foreach((get_the_category()) as $category) { $category->cat_name . ' '; } ?><a href="<?php echo get_category_link(get_cat_id($category->cat_name)); ?>"><?php echo $category->cat_name ?></a>

    This worked for me!

    It worked for me too!!

    I just wondering what is going to happen if I have more than one category assigned to one post? For example if I have “DOG” under “Animals” and “Four-legged”…

    Is there a way to choose between them?

    I have the same problem as anotherbreed. I am trying to list a category link of a post at the top of its single.php page but can’t figure out a way to exclude certain categories.

    This is the code I’ve got now:
    <a href="<?php echo $category_link; ?>" title="Category Name"><?php the_category(); ?></a>

    Thanks for any help offered!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘foreach get category link’ is closed to new replies.