简体   繁体   中英

PHP: Wordpress cycle through categories and output all posts images

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>

I'm confused on how to get this code correctly.

<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(array('category' => $cat->term_id));
                if ($posts) {
                    foreach ($posts as $p) {
                        echo '<option>';
                        echo get_the_post_thumbnail( $p->ID, 'medium' ).'<br>';
                        echo '</option>';
                    }
                }

                echo '</select>';
                $firstCat++;
            }
    ?>
</div>

notice i replaced $cat->term_id

if you are still getting nothing try hardcoding the category ID and see if you get any results.

try this..

add_action('init','test');
function test(){
    var_dump(get_posts(array('category' => 1)));    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM