• The theme that is being used it built from scratch, so not sure if there’s anything there that was missed.

    <?php $category_link = get_category_link( $category_id ); ?>
    <a href="<?php  echo $category_link; ?>" title="Category Source">/Category Source/</a>

    The above code doesn’t seem to work right, it just links back to the current post.

    <?php the_category(', '); ?> Works however..

    The goal is to find a way to get the link of the category belonging to a post.

    Would appreciate any help, thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • where did you define $category_id ?

    The goal is to find a way to get the link of the category belonging to a post.

    that is what <?php the_category(', '); ?> does perfectly;
    can you describe why you can’t/won’t use that function?

    Thread Starter Suffice

    (@suffice)

    Oh yeah, failed to copy the whole code…

    The problem is now found, yet not the solution.

    It would be ideal to use <?php the_category(', '); ?> if it was possible to change the title output:

    <a href="<?php the_category(', '); ?>" title="Category Source">/Category Source/</a>

    Which it doesn’t want to.

    failed to copy the whole code…

    please paste the full code into a https://pastebin.com/ and post the link to it here.

    example code for getting the categories of a post as links, separated by a space:

    <?php $cats = get_the_category($post->ID);
    foreach( $cats as $cat ) { ?>
    <a href="<?php echo get_category_link($cat->term_id); ?>"><?php echo $cat->name; ?></a>
    <?php } ?>
    Thread Starter Suffice

    (@suffice)

    Thanks, that worked out great!

    Modified it a bit:

    <?php $ep_cats = get_the_category($post->ID);
    foreach( $ep_cats as $ep_cat ) { ?>
    <a href="<?php echo get_category_link($ep_cat->term_id); ?>" title="<?php $ep_cat->name; ?>">/Category Source/</a> -|
    <?php } ?>

    For the other code, from before:

    <?php
        // Get the ID of a given category
        $category_id = get_cat_ID( 'Category Name' );
    
        // Get the URL of this category
        $category_link = get_category_link( $category_id );
    ?>
    
    <!-- Print a link to this category -->
    <a href="<?php echo $category_link; ?>" title="Category Name">Category Name</a>

    From the WordPress codex. As you can see, it relies on a category id, which get_the_category($post->ID) would have worked, I think…

    Well, thank you again–for your help!

    Suffice, thanks for posting your solution, not everyone does.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘get_category_link not working?’ is closed to new replies.