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 $cat) : $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( ...
🌐
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
... Appreciate all the answers... ... To store the name of the category instead of outputting it straight to the screen, use get_the_category() to retrieve the categories for the current post.
🌐
WisdmLabs
wisdmlabs.com › home › wordpress basics › how to create a link to current category in wordpress
How To Create A Link To Current Category In WordPress
April 7, 2014 - We then pass this $category_id as a parameter in the function called get_category_link() and store the result in $category_link. Here we are basically storing the link inside a variable so as to display it later using the HTML code.
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 ); ?>
Find elsewhere
🌐
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
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

🌐
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.
🌐
WordPress Codex
codex.wordpress.org › Linking_Posts_Pages_and_Categories
Linking Posts Pages and Categories « WordPress Codex
For example, to link to the category "testing" when the Category Base is "/index.php/categories", use the following link:
🌐
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] -&...