How to print the link of a category into the href=”” of an anchor tag in WordPress?

Ok so in my header.php file of my wordpress theme I have a navigation bar as shown below.

<nav>
    <a href="" class="nav-link" id="Design">Design</a>
    <a href="" class="nav-link" id="Tech">Tech</a>
    <a href="" class="nav-link" id="Photography">Photography</a>
    <a href="" class="nav-link" id="Film">Film</a>
    <a href="" class="nav-link" id="Gaming">Gaming</a>
    <a href="" class="nav-link" id="Motion">Motion</a>
    <a href="" class="nav-link" id="Lifestyle">Lifestyle</a>
    <a href="" class="nav-link" id="Coding">Coding</a>
    <a href="" class="nav-link" id="Business">Business</a>
    <a href="" class="nav-link" id="Other">Other</a>
</nav>

What I want to do is have a wordpress code that inserts the link to the page containing a list of all the categorys, in my case with the current permalink setup mine looks like this “www.example.com/design/post-name” so I essentially want it to link to “www.example.com/design” when the code is inserted into the anchor tag as shown below.

Read More
    <a href="<?php code I want goes here ?>" class="nav-link" id="Design">Design</a>

Any help is appreciated! 🙂

  • Jamie

Related posts

Leave a Reply

1 comment

  1. 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>