You need to get the category object and then use a foreach loop to build your list.

This should work for you:

<?php $categories = get_categories(); ?>
<nav>
  <?php foreach ($categories as $category):?>
    <a href="<?php echo get_category_link($category->term_id); ?>" class="nav-link" id="<?php echo $category->name;?>"><?php echo $category->name;?></a>
  <?php endforeach; ?>
</nav>
Answer from Hiram Hibbard on Stack Overflow
🌐
Wordpress
developer.wordpress.org › reference › functions › get_category_link
get_category_link() – Function | Developer.WordPress.org
You must log in to vote on the ... log in to vote on the helpfulness of this note ... <?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 ...
🌐
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 - <a href=”<?php echo $category_link ?>” title=”<?php echo $category_name ?>” > <?php echo $category_name ?> </a>[/pre]
Videos
April 24, 2022
1.13K
January 21, 2016
11.5K
August 25, 2015
8.82K
🌐
Stack Overflow
stackoverflow.com › questions › 22849124 › how-to-print-the-link-of-a-category-into-the-href-of-an-anchor-tag-in-wordpre
php - How to print the link of a category into the href="" of an anchor tag in Wordpress? - Stack Overflow

You need to get the category object and then use a foreach loop to build your list.

This should work for you:

<?php $categories = get_categories(); ?>
<nav>
  <?php foreach ($categories as $category):?>
    <a href="<?php echo get_category_link($category->term_id); ?>" class="nav-link" id="<?php echo $category->name;?>"><?php echo $category->name;?></a>
  <?php endforeach; ?>
</nav>
Answer from Hiram Hibbard on stackoverflow.com
🌐
Stack Exchange
wordpress.stackexchange.com › questions › 245303 › print-current-post-category-during-wp-query
wp query - Print current post category during WP_Query - WordPress Development Stack Exchange

You are using the get_the_terms() but this function is used to get the custom taxonomies.

Use the function get_the_category(); inside the WordPress loop and pass the get_the_ID() function which will get the current post ID in the loop.

get_the_category(get_the_ID());

                <?php
                  $args = array(
                    'post_type' => 'work',
                    'posts_per_page' => '9'
                  );
                  $work_loop = new WP_Query( $args );
                  if ( $work_loop->have_posts() ) :
                    while ( $work_loop->have_posts() ) : $work_loop->the_post();
                      // Set variables
                        $cat_ids                = get_the_ID();
                        $cat_names_array        = get_the_category($ids);
                        $work_category           = get_the_category( get_the_ID() );
                        $work_title             = get_field( 'work_title' );
                        $work_main_image        = get_field( 'work_main_image' );
                        $work_link              = get_field( 'work_title' );
                        $work_about             = get_field( 'work_about' );
                        // $work_category = get_the_terms( the_post()->ID, 'taxonomy' );
                      // Output
                      ?>
                      <a href="<?php echo $work_main_image['url']; ?>" class="single_item link <?php foreach ( $work_category as $key => $value) { echo $value->category_nicename . " "; } ?> col-md-4 col-sm-6 wow fadeInUp" data-wow-delay="0.3s">
                        <img src="<?php echo $work_main_image['url']; ?>" alt=""> 
                    </a>
                    <?php
                      endwhile;
                    wp_reset_postdata();
                  endif; 
                ?>

Cheers :)

Answer from Syed Fakhar Abbas on wordpress.stackexchange.com
🌐
WPExplorer
wpexplorer.com › tutorials › how to link to the current category in wordpress
How to Link to the Current Category in WordPress
September 26, 2023 - This tutorial will show you how to link to the current category for the post you are viewing in WordPress using a code snippet.
🌐
Stack Overflow
stackoverflow.com › questions › 20039661 › get-current-category-link
php - Get current category link - Stack Overflow

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 stackoverflow.com
🌐
Wordpress
developer.wordpress.org › reference › functions › get_the_category
get_the_category() – Function | Developer.WordPress.org
You must log in to vote on the helpfulness of this noteVote results for this note: 9You must log in to vote on the helpfulness of this note · Show All Categories as Links This outputs all the categories assigned to the post as links. Must be used inside the loop.
🌐
Wordpress
codex.wordpress.org › Linking_Posts_Pages_and_Categories
Linking Posts Pages and Categories « WordPress Codex
When doing so, you may safely omit ... and link to the target with a full path: ... The leading slash means "At the very top of this domain is a directory named wordpress, and inside this directory is a file named index.php". ... The lack of a leading slash means "Inside the current directory ...
Find elsewhere
🌐
Stack Exchange
wordpress.stackexchange.com › questions › 38237 › how-to-get-the-category-of-the-post-and-link-it-to-the-archive-of-the-category
categories - How to get the category of the post and link it to the archive (of the category) - WordPress Development Stack Exchange

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.

Answer from its_me on wordpress.stackexchange.com
🌐
Stack Overflow
stackoverflow.com › questions › 14816552 › wordpress-get-category-link
php - Wordpress get category link - Stack Overflow

Sounds like you may want get_category_link - something like:

$categories = get_categories();
foreach ($categories as $cat) {
    $category_link = get_category_link($cat->cat_ID);
    echo '<a href="' . esc_url($category_link) . '" title="' . esc_attr($cat->name) . '">' . esc_html($cat->name) . '</a>';
}

should print out the links to the categories for you.

Answer from anotherthink on stackoverflow.com
🌐
Stack Exchange
wordpress.stackexchange.com › questions › 219954 › how-do-i-get-the-category-url-from-get-the-category
categories - How do I get the category URL from get_the_category? - WordPress Development Stack Exchange

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(); ?>
Answer from Adam on wordpress.stackexchange.com
🌐
WPMU DEV Blog
wpmudev.com › home › miscellaneous › how to get a wordpress category name without the link
How to Get a WordPress Category Name Without the Link - WPMU DEV
March 11, 2022 - ' '; } ?> category. I will also put <strong> tags around it to make it bold, so the complete line I am inserting will look like this: <strong>This post is from the <?php foreach((get_the_category()) as $category) { echo $category->cat_name .
🌐
Wordpress
developer.wordpress.org › reference › functions › get_categories
get_categories() – Function | Developer.WordPress.org
List Categories and Descriptions This example will list, in alphabetic order, all categories presented as links to the corresponding category archive. Each category description is listed after the category link. <?php $categories = get_categories( array( 'orderby' => 'name', 'order' => 'ASC' ...
🌐
Stack Exchange
wordpress.stackexchange.com › questions › 108413 › direct-link-to-category
categories - Direct link to category? - WordPress Development Stack Exchange

There is a function called get_category_link() which might be helpful for you. This will be able to generate an appropriate link without having to hard-code it, except for the category name or ID.

Examples from the Wordpress Codex:

<?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( $category_link ); ?>" title="Category Name">Category Name</a>

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

Answer from Ben Cole on wordpress.stackexchange.com
🌐
Stack Exchange
wordpress.stackexchange.com › questions › 131749 › display-current-category-title-on-category-page
php - Display current category title on category page - WordPress Development Stack Exchange

On a category page, you can use the function single_cat_title(), or the more generic single_term_title(). These functions pull from the global $wp_query object, via get_queried_object().

Answer from Milo on wordpress.stackexchange.com
🌐
Stack Overflow
stackoverflow.com › questions › 1735953 › display-link-to-category-in-wordpress
Display Link to Category in Wordpress? - Stack Overflow

If you want to do this on post page you can add something like the following to your single.php file of your theme.

<div class="meta">Posted in: <span><?php the_category(', ') ?> </span></div>
Answer from gbennett on stackoverflow.com
🌐
Stack Exchange
wordpress.stackexchange.com › questions › 133867 › how-to-display-a-list-of-categories
How to display a list of categories - WordPress Development Stack Exchange

I will show you how to display a list of categories in WordPress and also mark the active category, see code below:

<?php
// Get the current queried object
$term    = get_queried_object();
$term_id = ( isset( $term->term_id ) ) ? (int) $term->term_id : 0;

$categories = get_categories( array(
    'taxonomy'   => 'category', // Taxonomy to retrieve terms for. We want 'category'. Note that this parameter is default to 'category', so you can omit it
    'orderby'    => 'name',
    'parent'     => 0,
    'hide_empty' => 0, // change to 1 to hide categores not having a single post
) );
?>

<ul>
    <?php
    foreach ( $categories as $category ) 
    {
        $cat_ID        = (int) $category->term_id;
        $category_name = $category->name;

        // When viewing a particular category, give it an [active] class
        $cat_class = ( $cat_ID == $term_id ) ? 'active' : 'not-active';

        // I don't like showing the [uncategoirzed] category
        if ( strtolower( $category_name ) != 'uncategorized' )
        {
            printf( '%3$s',
                esc_attr( $cat_class ),
                esc_url( get_category_link( $category->term_id ) ),
                esc_html( $category->name )
            );
        }
    }
    ?>
</ul>

Notes about the code above:

get_queried_object() retrieve the currently-queried object. For example:

  • if you're on a single post, it will return the post object
  • if you're on a page, it will return the page object
  • if you're on an archive page, it will return the post type object
  • if you're on a category archive, it will return the category object
  • if you're on an author archive, it will return the author object
  • etc.

But there are some implications when using get_queried_object(), you should not expect it to return a post type object even when is_post_type_archive() is true. Check it out for more info.

Also, note that get_queried_object() is a wrapper for $wp_query->get_queried_object(), so it returns a WP object data type.

get_categories()

get_categories() retrieve list of category objects. Currently accepts only one parameter - $args. The $args parameter specifies a list of arguments that should be used to retrieve categories. See get_terms() for additional options.

However, to get the category for a particular posts here's a simple function I wrote: How to get list of categories for a post

Answer from John Zenith on wordpress.stackexchange.com
🌐
Stack Exchange
wordpress.stackexchange.com › questions › 59476 › get-current-category-id-php
get current category ID php - WordPress Development Stack Exchange

you can use get_queried_object()

$category = get_queried_object();
echo $category->term_id;
Answer from Milo on wordpress.stackexchange.com
🌐
WPBeginner
wpbeginner.com › home › tutorials › how to display category descriptions in wordpress
How to Display Category Descriptions in WordPress
December 4, 2024 - Do you want to display category descriptions on your WordPress site? Learn how to easily add and display category descriptions in WordPress.
🌐
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 - WordPress Get Post Category - Display category of the post in WordPress. Get the current post category name, slug, or URL in WordPress.