• I have a bootstrap “tab” system going on where each tab is it’s own category name:

    <?php $categories= get_categories();
        $firstCat = 1;
        foreach ($categories as $cat) {
            $trimmedCatName = str_replace(' ', '', $cat->cat_name);
            echo '<li';
            if ($firstCat == 1) {
                echo ' class="active"';
            }
            echo '>'.'<a href="#'.$trimmedCatName.'"  data-toggle="tab">'.$cat->cat_name.' <small style="color:#447294">('.$cat->category_count.')</small></a></li>';
            $firstCat++;
        }
    ?>

    This above ^ code works fine and sets the tabs up nicely.

    The problem I have is with cycling through the categories as “tab-content” and then for each separate category, showing all the post titles/images for that category. Here’s what I have so far:

    <div class="tab-content">
        <?php $categories= get_categories();
                $firstCat = 1;
                foreach ($categories as $cat) {
                    $trimmedCatName = str_replace(' ', '', $cat->cat_name);
                    echo '<div class="tab-pane ';
                    if ($firstCat == 1) {
                        echo 'active';
                    }
                    echo '" id="#'.$trimmedCatName.'">'.
                    '<select class="image-picker">';
    
                    $posts = get_posts($cat);
                    if ($posts) {
                        foreach ($posts as $p) {
                            echo '<option>';
                            echo get_the_post_thumbnail( $p->ID, 'medium' ).'<br>';
                            echo '</option>';
                        }
                    }
    
                    echo '</select>';
                    $firstCat++;
                }
        ?>
    </div>
  • The topic ‘PHP: WordPress cycle through categories and output all posts images’ is closed to new replies.