Forums

id and name of category and parent (1 post)

  1. PontusD
    Member
    Posted 4 months ago #

    Hi!

    I have three levels in my categorys. Just like this:
    - First
    -- Second
    --- Third

    I want ID of the second category and the name of the third when listing the posts. This is what i've come up with so far. As you can see it will give me the correct ID (of second) but "wrong" name (it will give me the name of the third, but only 1 category of the third level, it could have several).

    <ul>
       <?php
       global $post;
       $myposts = get_posts('orderby=title&order=asc&numberposts=-1');
               foreach( $myposts as $post ) : setup_postdata($post);
               foreach((get_the_category()) as $category) {
    		$categories = get_the_category($post->ID);
       ?>
       <a href="<?php the_permalink(); ?>" class="<?php echo $category->cat_ID; ?>"><li><span><?php echo $categories[0]->cat_name; ?></span> <span><?php echo ( category_description($categories[0]->cat_ID) );  ?></span></li></a>
       <?php  } endforeach; ?>
    </ul>

    Then I did something like this:

    <?php
    	$cat_id = get_query_var('cat');
    	$catlisting = get_categories('child_of=' . $cat_id);
    	foreach($catlisting as $catlist) { ?>
    	<a href="<?php the_permalink(); ?>" class="<?php echo $catlist->cat_ID; ?>"><li><span><?php echo $catlist->cat_name; ?></span> <span><?php echo ( category_description($catlist->cat_ID) );  ?></span></li></a>
    	<?php  }  ?>

    This will give me the correct name but the wrong ID (name and ID of third level)

    Does anyone understand what I want?

Reply

You must log in to post.

About this Topic