• Hi my wordpress link structure is like this>

    https://www.site.com/Category/Pagename

    When I’m on “Pagename” I’d like a link that links back to “Category”.

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

    I thought this code might work. It displays the “Category” link correctly but links to the current page instead of “Cateogry”.

    Anyone know PHP well enough to be able to modify it so the link goes back to “Category”?

    I’ve spent hours trying to find a solution in the Codex so this is my last resort.

    Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter hamburger

    (@hamburger)

    I guess I could also use

    <?php the_category(' '); ?>

    But that just displays the Category name…maybe is there a way possible so I could add text and make the whole line a hyperlink?

    Thread Starter hamburger

    (@hamburger)

    Okay last question. Getting there…this code works the best:

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

    However how do I put in a dynamic word in front of “Projects” so it changes depending on the category?

    Thread Starter hamburger

    (@hamburger)

    <?php
    foreach((get_the_category()) as $category) {
        echo '<a href="'.get_category_link($category->cat_ID).'"> < Return To '.($category->name).' Projects</a>';
    }
    ?>
    Thread Starter hamburger

    (@hamburger)

    So I tweaked this a little more…

    <?php
    foreach((get_the_category()) as $link) {
        echo '<a href="'.get_cat_ID($link->cat_ID).'"> < Return To '.($link->name).' Projects</a>';
    }
    ?>

    it’s better but now all my links look like this:

    https://www.site.com/Category/0

    with the “0” on the end. How do I get rid of the “0” so my link just displays the category?

    Hamburger,
    I know this post has been up forever, but I was looking to do the same thing.
    If you still need to know, or are still curious, or someone else stumbles upon this…
    what you want is:

    echo '<a href="'.$link->slug.'"> < Return To '.$link->name.' Projects</a>

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Back link on post based on current category’ is closed to new replies.