WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Getting Permalink For Sub Category In Loop (2 posts)

  1. Gemfruit
    Member
    Posted 9 months ago #

    I'm looping through all of the sub-categories of a certain parent category, and am displaying a bunch of information about those sub-categories. I can get the description, name, id, etc, but I can't seem to pin the permalink.

    Here's my code:

    <h2 class="title" id="post-<?php $child->cat_ID; ?>"><a href="<?php get_permalink(get_page_by_title($child->name)); ?>" rel="bookmark" title="Permanent Link to <?php echo($child->name); ?>"><?php echo($child->name); ?></a></h2>

    $child is the sub-category, and that code is part of a foreach loop. I've tried numerous attempts to get the permalink, but I keep getting -

    site.com/category

    instead of

    site.com/category/sub-category

    Any idea what's wrong with my code?

  2. Gemfruit
    Member
    Posted 9 months ago #

    I've discovered the solution myself, because knowledge is power!

    Here's my working code, for anyone interested.

    <?php $descendants = get_categories(array('child_of' => 2));
    		foreach ($descendants as $child) { ?>
    
    			<div id="post-<?php $child->cat_ID; ?>" <?php post_class('full-width-excerpt'); ?>>
    				<a href="<?php echo(get_category_link($child->cat_ID)); ?>" <?php the_post_thumbnail(array(260,200), array("src" => z_taxonomy_image_url($child->term_id), "class" => "alignleft post_thumbnail")); ?> </a>
    				<h2 class="title" id="post-<?php $child->cat_ID; ?>"><a href="<?php echo(get_category_link($child->cat_ID)); ?>" rel="bookmark" title="Permanent Link to <?php echo($child->name); ?>"><?php echo($child->name); ?></a></h2>
    
    				<div class="entry">
    				<?php echo($child->category_description); ?>
    				</div>
    			</div>
    	<?php
    		}
    	?>

Reply

You must log in to post.

About this Topic