WordPress – Permalink of sub-category in loop

I’ve got a template looping through each of the sub-categories in the main category you’ve selected. The title of each I would like hyperlinked, so if you select a sub-category you can now go to that specific categories posts.

However when using <?php get_category_link( $category_id ); ?>, it outputs the permalink of the parent category instead.

Read More
<div class="row">
<?php $i = 1; $categories = get_categories("child_of=$cat"); foreach ($categories as $cat) { ?>
    <div class="col-md-6">
        <?php query_posts("cat=$cat->cat_ID&posts_per_page=1&order=ASC&orderby=name"); ?>
        <div class="top-category-container">
            <div class="category-label">
                <span>Latest From</span>
                <h2><a href="#"><?php single_cat_title(); ?></a></h2>
            </div>
            <?php while (have_posts()) : the_post(); ?>
                <div class="post">
                    <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
                    <div class="article-img">
                        <a href="<?php the_permalink(); ?>"><?php echo the_post_thumbnail('', array('class' => 'img-responsive')); ?></a>
                    </div>
                    <p><?php the_excerpt(); ?></p>
                </div>
            <?php endwhile; ?>
        </div>
    </div>
<?php } ?> 
</div>

LIVE URL: http://dev.madd-cow.com/category/man-rehab/

Related posts

Leave a Reply

1 comment

  1. Change

    <h2><a href="#"><?php single_cat_title(); ?></a></h2>
    

    to

    <h2><a href="<?php $cat = get_term_by('name', single_cat_title('',false), 'category'); echo $cat->slug; ?>"><?php single_cat_title(); ?></a></h2>