PHP:Wordpress循环浏览类别并输出所有帖子的图像


PHP: Wordpress cycle through categories and output all posts images

我有一个引导程序"选项卡"系统,每个选项卡都有自己的类别名称:

<?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++;
    }
?>

上面的^代码运行良好,并很好地设置了选项卡。

我遇到的问题是,将类别作为"选项卡内容"进行循环,然后为每个单独的类别显示该类别的所有帖子标题/图像。到目前为止,我拥有的是:

<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>

我对如何正确获取此代码感到困惑。

<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>

注意,我替换了$cat->term_id

如果你仍然一无所获,试着对类别ID进行硬编码,看看你是否得到了任何结果。

试试这个。。

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