Forums

[resolved] Getting SubCategories of the Post in the loop (6 posts)

  1. sdjentertainment
    Member
    Posted 1 year ago #

    Hey Hey,

    So I've been trying to figure out this problem that I've been having and wonder if anybody has any suggestions.

    Inside my index.php I'm filtering the loop in different categories. I've set it up as the following

    Projects(Main Category)
    - Music (sub-category)
    - Video (sub-category)
    - Audio (sub-category)

    Sponsors(Main Category)
    - Music (sub-category)
    - Video (sub-category)
    - Audio (sub-category)

    While I'm going through the loop I need to display just the sub-categories of the post that it's currently looping through and display it as a class to the

  2. element it's housed in.

    I've been getting it to list all the categories, but NOT just the sub-categories of the blog that it's looping through.

    So for example:
    If I have a blog post with the main Category of "Sponsors" and the sub-categories of "Video & Music". I want it to come out as

    <li class="video music">...</li>

    I'm basically trying to incorporate the jQuery Quicksand plugin to my blog

    Any Suggestions?

  • alchymyth
    The Sweeper
    Posted 1 year ago #

    what you didn't like with the ealier attempt comes from wordpress' way of naming the slugs of categories if the category names appear double as subcategories of different parents;
    this new code uses the category names and turns them into a sanitized form, so the names are lowercase with hyphens (instead of spaces - similar to slugs)

    <?php $cats = get_the_category($post->ID);
    $subcategories = array();
    foreach( $cats as $cat ) {
      $subcats = get_categories('child_of='.$cat->term_id);
      if($subcats) {
        foreach( $subcats as $subcat )
          {  $subcategories[] = sanitize_title($subcat->name); }
      }
    }
    $subcats = array_unique($subcategories);
    if($subcats) {
      $subcats = implode(' ',$subcats);
      echo 'class="' . $subcats . '"';
    }
    ?>
  • sdjentertainment
    Member
    Posted 1 year ago #

    Appreciate taking your time to help me out. Thanks

    When I run the code, it gives me all the sub-categories inside of the main category. I'm just trying to find the sub-categories that are selected for each post in the loop...

    ...
    <?php query_posts('cat=14&showposts=3'); ?>
    <?php $posts = get_posts('category=14&numberposts=3&offset=0');  ?>
    <?php foreach ($posts as $post) : start_wp(); ?>
    
    <?php $cats = get_the_category($post->ID);
        $subcategories = array();
        foreach( $cats as $cat ) {
        $subcats = get_categories('child_of='.$cat->term_id);
        if($subcats) {
        foreach( $subcats as $subcat )
          {  $subcategories[] = sanitize_title($subcat->name); }
        }
        }
        $subcats = array_unique($subcategories);
        if($subcats) {
        $subcats = implode(' ',$subcats);
        echo '<li class="' . $subcats . '">';
        }
        ?>
    .......
    
    <?php endforeach ?>
    ...

    If a blog post is in the main category of "Projects" and has 2/4 sub-categories, display the 2 sub-categories.

  • alchymyth
    The Sweeper
    Posted 1 year ago #

    i think i'm getting it now:

    you only want to show the category name of the post's category which is also at sub-category level - as a css class in the li tag that surrounds your post.

    last try, in the context of your code:

    <?php $posts = get_posts('category=14&numberposts=3&offset=0');  ?>
    <ul>
    <?php foreach ($posts as $post) : start_wp(); ?>
    <li <?php $cats = get_the_category($post->ID);
    $subcats = array();
    foreach( $cats as $cat ) {
      $cat = get_category($cat->term_id);
      if($cat->parent) { $subcats[] = sanitize_title($cat->name); }
    }
     if($subcats) { echo 'class="';
     foreach($subcats as $subcat) { echo $subcat . ' '; }
     echo '"';
    }?>>
    .......
    </li>
    <?php endforeach ?>
    </ul>
    ...
  • sdjentertainment
    Member
    Posted 1 year ago #

    YES!

    Brilliant, thanks a lot alchymyth.

    Cheers,
    Simon

  • alchymyth
    The Sweeper
    Posted 1 year ago #

    you're welcome.

    'third time lucky' ;-)

  • Topic Closed

    This topic has been closed to new replies.

    About this Topic

    Tags