get_* functions are return functions. They don't actually print anything. Given your code example, all you should need is an echo statement:

<?php 
global $post;
$category = reset(get_the_category($post->ID));
$category_id = $category->cat_ID;
?>

<a class="button" href="<?php echo get_category_link( $category_id ); ?>">&larr; Back to Portfolio</a>

ALTERNATIVE: The best way of doing this would be to keep everything in your loop for reuse in single or archive views, combined with the use of wp_get_post_categories. This is untested, but it should get you started with a general-use method of listing categories associated with a given post or set of posts:

<?php
if(have_posts()) : while(have_posts()) : the_post();
    $cats = wp_get_post_categories($post->ID);
    if($cats) : foreach($cats as category = get_category($cat);
    ?>
    <a class="button" href="<?php echo get_category_link($category->cat_ID); ?>">&larr; <?php echo $category->name ?></a>
    <?php
    endforeach;endif;
endwhile;endif;
?>
Answer from maiorano84 on Stack Overflow
🌐
WordPress
developer.wordpress.org › reference › functions › get_category_link
get_category_link() – Function | Developer.WordPress.org
<?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 esc_url( ...
Discussions

php - How to print the link of a category into the href="" of an anchor tag in Wordpress? - Stack Overflow
Ok so in my header.php file of my wordpress theme I have a navigation bar as shown below. More on stackoverflow.com
🌐 stackoverflow.com
php - Wordpress get category link - Stack Overflow
I want to ask on how to get the link of each category that have related post on it. I only got some code that will only display parent category. Any help would be much appreciated. Thanks! More on stackoverflow.com
🌐 stackoverflow.com
wp query - Print current post category during WP_Query - WordPress Development Stack Exchange
How do I print each value containing the category name separated by a space....? I got one step closer and updated my code above. ... Nice I had to change one part of it: Now, how do I add a space between each value? $value) { echo $value->category_nicename; } ?> It currently returns: class="single_item link ... More on wordpress.stackexchange.com
🌐 wordpress.stackexchange.com
December 17, 2016
categories - How do I get the category URL from get_the_category? - WordPress Development Stack Exchange
My loop below shows the latest 4 posts from the same category as the post currently being viewed. Its located within single.php. I'm trying to get the URL of that same category so I can link back... More on wordpress.stackexchange.com
🌐 wordpress.stackexchange.com
🌐
WisdmLabs
wisdmlabs.com › home › technology › how to create a link to current category in wordpress
How To Create A Link To Current Category In WordPress
April 7, 2014 - <a href=”<?php echo $category_link ?>” title=”<?php echo $category_name ?>” > <?php echo $category_name ?> </a>[/pre]
🌐
DaniWeb
daniweb.com › programming › web-development › threads › 265806 › get-the-category-link-inside-the-loop-in-wordpress
Get The Category Link Inside The Loop In WordPress - php
March 8, 2010 - I think getting the link will depend on your permalinks settings: <?php $current_post_categories = get_the_category(); // store category objects for current post in an array foreach( $current_post_categories as $category) { echo $category->cat_name; ...
🌐
WPExplorer
wpexplorer.com › home › tutorials › how to link to the current category in wordpress
How to Link to the Current Category in WordPress
September 26, 2023 - Displaying a link to the current post’s category in WordPress is easy to do and I will show you how you can do it via 3 different methods: using custom code, using the Total WordPress theme or via the Yoast SEO breadcrumbs. Simply paste this code wherever you want your category link to appear. This will display a link to the first category of your post. This code can be placed in any theme template file, even outside the loop, but it won’t work when placed in functions.php unless it’s hooked into an action hook that runs once WordPress has initialized such as the wp hook.
Find elsewhere
🌐
CodexWorld
codexworld.com › home › how to guides › how to get post category name and url in wordpress
How to Get Post Category Name and URL in WordPress - CodexWorld
November 20, 2015 - $categories = get_the_category(); $separator = ' '; $output = ''; if ( ! empty( $categories ) ) { foreach( $categories as $category ) { $output .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) .
Top answer
1 of 2
9

Use:

get_category_link( $category_id );

See:

https://codex.wordpress.org/Function_Reference/get_category_link

In your specific case:

<?php
global $post;
$categories = get_the_category();

    foreach ($categories as $category) :

       $exclude = get_the_ID();
       $posts = get_posts('posts_per_page=4&category='. $category->term_id);

        foreach($posts as $post) :
         if( $exclude != get_the_ID() ) { ?>

                <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="post c-1"> Link to actual post</a>

    <?php } endforeach; ?>

<a href="<?php echo esc_url( get_category_link( $category->term_id ) ); ?>" title="View all" class="btn border"><i class="i-right-double-arrow"></i> View all <?php echo $category->name; ?></a>
<?php  endforeach; wp_reset_postdata(); ?>
2 of 2
1

A STRAIGHT FORWARD AND CLEAN CODE

I'm a newbie :)

I modified the codes given by Adam and removed the unnecessary parts not needed to answer the initial question.

It worked for me 100%.

Give it a try.

Please let me know if it worked for you too :)

<?php $categories = get_the_category();
foreach ($categories as $category) :
endforeach; ?>

<a href="<?php echo esc_url( get_category_link( $category->term_id ) ); ?>">
  LINK TO CURRENT POST CATEGORY >>
</a>

OR

<?php $categories = get_the_category();
foreach ($categories as $category) : ?>

The category is:
<a href="<?php echo esc_url( get_category_link( $category->term_id ) ); ?>">
  <?php echo $category->name; ?>
</a>

<?php endforeach; ?>

COMBINE SOME OTHER ELEMENTS AS YOU LIKE. PLAY AROUND BUDDY.

Top answer
1 of 2
1

You can use get_queried_object(), which will return category object.

See documentation:

https://codex.wordpress.org/Function_Reference/get_queried_object

2 of 2
0

Put your $c term (in your loop) in a print_r/var_dump to see if the property you are trying to compare the values of is of the actual value that you are looking for (See below).

I've also added a ternary operator in the place of your if statement, it is just better practice. (Read below for more on ternary operators).

<menu id="nav">
    <ul>   
    <?php $cat_id = get_cat_ID();
    foreach( $categories as $c ):?>
        <?php print_r($c); ?>         
        <li class="<?php echo $c->term_id == $cat_id ? 'active' : null ;} ?>">
        <a href="<?php echo get_category_link( $c->term_id ); ?>" title="<?php echo $c->cat_name ;?>">
        <?php echo $c->cat_name ;?>
        </a>                
        </li>
    <?php endforeach;   ?>  
    </ul>
</menu>

Ternary Operator

In computer programming, ?: is a ternary operator that is part of the syntax for a basic conditional expression in several programming languages. It is commonly referred to as the conditional operator, inline if (iif), or ternary if.

if(1==1) echo 'true'; 
else echo 'false';

can be done in a ternary operator like so:

echo 1==1 ? 'true' : 'false';

Another example

if(1==1) $boolean = true;
else $boolean = false;

can be done in a ternary operator like so:

$boolean = 1==1 ? true : false;

Since PHP 5.3, the 'middle part' of the operator can be left out. Shorthand ternary operators can be used to match only false

if(1!=2) echo 'false';

The ternary operator:

echo 1!=2 ?: 'false';

Read more: http://php.net/manual/en/language.operators.comparison.php

🌐
Stack Overflow
stackoverflow.com › questions › 58146498 › how-to-link-a-post-to-its-category-in-wordpress
How to link a post to its category in WordPress - Stack Overflow
<?php $qry = new WP_Query($args ); ?> <?php if ( $qry->have_posts() ) : ?> <?php while ( $qry->have_posts() ) : $qry->the_post(); $postcat = get_the_category();?> <div class="hometile"> <a href="<?php echo get_category_link( $postcat[0]->term_id ); ?>"> <?php echo $postcat[0]->name; ?> <?php the_post_thumbnail(); ?> </a> </div> <?php endwhile; ?> <?php endif; ?>
Top answer
1 of 2
7

One these three should do the job for you...

1. Function: the_category();

News su <?php the_category(', '); ?>

Displays as:

News su WordPress, Computers, Blogging

And if only a single category is assigned to a post, it shows up like this:

News su WordPress

2. Function: get_the_category_list();

<div id="pagine"><?php echo get_the_category_list(); ?></div>

Displays as:

<div id="pagine">
    <ul class="post-categories">
        <li>
            <a href="http://example.com/category/wordpress/" title="View all posts in Business" rel="category tag">WordPress</a>
        </li>
        <li>
            <a href="http://example.com/category/computers/" title="View all posts in Business" rel="category tag">Computers</a>
        </li>
    </ul>
</div>

And if only a single category is assigned to a post, the output would be like this:

<div id="pagine">
    <ul class="post-categories">
        <li>
            <a href="http://example.com/category/wordpress/" title="View all posts in Business" rel="category tag">WordPress</a>
        </li>
    </ul>
</div>

3. Function: single_cat_title();

If you want to show just one category (category with the lowest ID will be shown) no matter how many categories are assigned to a post, use something like this:

<div id="pagine">
    <ul>
        <li>
            <?php
                $category = get_the_category();
                echo '<a href="'.get_category_link($category[0]->cat_ID).'">News su ' . $category[0]->cat_name . '</a>';
            ?>
        </li>
    </ul>
</div>

The above code always shows one category, like this:

News su WordPress

So, given the codes (and what each does), suit them to your needs.

2 of 2
1

get_the_category returns an array of the stdClass object of all categories associated with the current post_id.

$category[0] will give you the first category in the array. To see the mapping do.

<?php print_r( $category ); ?>
🌐
WordPress
developer.wordpress.org › reference › functions › get_the_category
get_the_category() – Function | Developer.WordPress.org
$categories = get_the_category(); if ( ! empty( $categories ) ) { echo '<a href="' . esc_url( get_category_link( $categories[0]->term_id ) ) . '">' . esc_html( $categories[0]->name ) . '</a>'; } ... You must log in to vote on the helpfulness of this noteVote results for this note: 16You must log in to vote on the helpfulness of this note · Get the post category if you have a custom post_type · <?php /* FIRST * Note: This function only returns results from the default “category” taxonomy.
🌐
Stack Overflow
stackoverflow.com › questions › 39289065 › how-to-print-some-wordpress-cateogries
php - How to Print some WordPress Cateogries? - Stack Overflow
I've the following code in attempt to print the second top category and the category of a search result: $category = get_the_terms( $post_id , 'manualknowledgebasecat'); $number = $category[0] -&...