0

here's what I'm trying to accomplish:

I've got several categories, each of them have several posts. Now, on category.php I'm able to output a list of all categories and, inside this list, show a list of the posts of the current category.

  • Cars (class: currentcat)
    • Hyunday
    • BMW
    • Mercedes
  • Architects
  • Popular First Names
  • Football Teams

Actually I output the specific posts for each category but hide them via CSS and show only those of currentcat. When viewing a single post on single.php (eg "Mercedes"), I want the same, but for all categories assigned:

  • Cars (class: currentcat)
    • Hyunday
    • BMW
    • Mercedes
  • Architects
  • Popular First Names (class: currentcat)
    • Steve
    • Matilda
    • Mercedes
  • Football Teams

(Please pardon this excellent example.) On category.php I'm using:

 <ul>
      <?php
      $cargs = array(
        'type'                     => 'post',
        'child_of'                 => 0,
        'parent'                   => '',
        'orderby'                  => 'name',
        'order'                    => 'ASC',
        'hide_empty'               => 0,
        'hierarchical'             => 1,
        'exclude'                  => '1',
        'include'                  => '',
        'number'                   => '',
        'taxonomy'                 => 'category',
        'pad_counts'               => false 
      ); 
      $categories=get_categories($cargs);
      foreach($categories as $category) {
           if($cat != $category->term_id) {
                echo '<li class="zk_category-item"><a href="'.get_category_link($category->cat_ID).'">'.$category->cat_name."</a><ul>";
            }
            else {
                echo '<li class="currentcat"><a href="'.get_category_link($category->cat_ID).'" class="activelink">'.$category->cat_name."</a><ul>";
            }
           $category_posts=get_posts('category='.$category->cat_ID);
           foreach($category_posts as $post) {
                echo '<li class="zk_project-item"><a href="'.$post->guid.'">'.$post->post_title.'</a></li>';
            };
           echo "</ul></li>";
        };
        ?>
 </ul>

I see why this can't work on single.php, but I have no idea how to adjust it to make it work for single posts which might have several categories assigned.

I thought about and tried getting an array of all assigned categories of the current post on single.php and checking whether $category->term_id appears in this array, but to no avail.

Any help is highly appreciated!

0

Solved it with following code. My idea posted above worked out, but I had to rearrange the assigned categories in a new array.

<ul>
<?php
    $cargs = array(
        'type'                     => 'post',
        'child_of'                 => 0,
        'parent'                   => '',
        'orderby'                  => 'name',
        'order'                    => 'ASC',
        'hide_empty'               => 0,
        'hierarchical'             => 1,
        'exclude'                  => '1',
        'include'                  => '',
        'number'                   => '',
        'taxonomy'                 => 'category',
        'pad_counts'               => false 

    ); 
    $categories = get_categories($cargs);
    $post = $wp_query->post;
    $postid = $post->ID;
    $postcategories = get_the_terms($postid, 'category');
    $postcategoriesids = array();
    foreach ($postcategories as $postcategory) {
        $postcategoriesids[] = $postcategory->term_id;
    };
    foreach($categories as $category) {
        if(in_array($category->term_id, $postcategoriesids)) {
            echo '<li class="currentcat"><a href="'.get_category_link($category->cat_ID).'" class="activelink">'.$category->cat_name."</a><ul>";
        }
        else {
            echo '<li class="zk_category-item"><a href="'.get_category_link($category->cat_ID).'">'.$category->cat_name."</a><ul>";
        }
        $category_posts=get_posts('category='.$category->cat_ID);
        foreach($category_posts as $post) {
            echo '<li class="zk_project-item"><a href="'.$post->guid.'">'.$post->post_title.'</a></li>';
        };
        echo "</ul></li>";
    };
?>


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.