WordPress:显示所有类别,但是如果一个类别仅包含一个帖子,则改为显示此帖子

问题描述

我对wordpress(和PHP)还是陌生的,我正在尝试完成我的第一个模板。 当前的请求是显示所有给定的(子)类别,但是如果一个类别只有一个帖子,则应该显示该帖子而不是摘录。我想我只想念一小块就能完成这个工作...

到目前为止我所拥有的:

<?PHP 
     if(!empty($categories)) { ?>
               
         <!-- display Sub-Categories and description -->
         <div class="brick_list">
            <?PHP                   
                 foreach ( $categories as $category ) {
                     echo '<div class="item"><h4 class="item-title">' . $category->name . '</h4><div class="item-text">' . $category->description . '</div><div class="item-link"><a href="' . get_category_link( $category->term_id ) . '">Read more</a></div></div>';
                 }
             ?>
         </div>

<?PHP }; ?>

我已经在网上搜索了“类别中只有一个帖子”任务的解决方案,发现了这一点:

if( 1 == $category[0]->count ) { ..... }

但是我不知道如何在现有的foreach循环中包含(或合并)它。 有人可以帮忙吗?

解决方法

我对您的代码进行了一些编辑,我添加了一个条件,以便在将div元素浸入之前检查类别计数。

<?php

$categories = get_categories();

if(!empty($categories)) { ?>
               
         <!-- Display Sub-Categories and description -->
         <div class="brick_list">
            <?php                   
                 foreach ( $categories as $category ) {
                     if ( $category->count!= 1 ){
                     echo '<div class="item"><h4 class="item-title">' . $category->name . '</h4><div class="item-text">' . $category->description . '</div><div class="item-link"><a href="' . get_category_link( $category->term_id ) . '">Read more</a></div></div>';
                 } else {
                     // display the post with excerpt
                     }
                 }
                         
            
             ?>
         </div>

<?php }; ?>

?>

让我知道您是否遇到任何问题。

,

这是我的最新尝试,但由于我仅收到错误500(内部服务器错误),因此我似乎仍然缺少这里的内容。有想法吗?

<?php                   
     foreach ( $categories as $category ) {
        if( $category->count == 1) { ?>
            <div class="brick_item"><h4 class="brick_item-title"><?php the_title(); ?></h4><div class="brick_item-text"><?php echo wp_trim_words( get_the_content(),30,'...'); ?></div><div class="brick_item-link"><a href="<?php the_permalink();?>" class="readmore">Read more</a></div></div>
        <?php } else {
            echo '<div class="brick_item"><h4 class="brick_item-title">' . $category->name . '</h4><div class="brick_item-text">' . wp_trim_words($category->description,'...') . '</div><div class="brick_item-link"><a href="' . get_category_link( $category->term_id ) . '" class="readmore">Read more</a></div></div>';
        }
     }
?>
,

我能够解决它。最后,它看起来并不困难:-D 我在这里分享,以防别人需要它

<?php                   
     foreach ( $categories as $category ) {
                               
         // If there is only one post available,go directly to the post
         if($category->count == 1) {
            $all_posts = get_posts($category);
            echo '<div class="item"><h4 class="item-title">' . get_the_title($all_posts[0]->ID) . '</h4><div class="item-text">' . wp_trim_words( get_the_content($all_posts[0]->ID),'...') . '"</div></div>';
         
         // otherwise display subcategories
         } else {
            echo '<div class="item"><h4 class="item-title">' . $category->name . '</h4><div class="item-text">' . wp_trim_words($category->description,'...') . '</div></div>';
         }
    }
?>