如何使用循环显示我的所有类别名称(WordPress) [英] How to display my all category names using loop (WordPress)

查看:55
本文介绍了如何使用循环显示我的所有类别名称(WordPress)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有以下代码。显示的是我也需要类别名称和描述。现在,我需要显示他们在类别中的帖子。我该怎么做?

I have below code here. It's display that I want category names and description as well. Now I need to display post that they have inside their categories. How I do it?

<?php 
    $args = array(
        'orderby' => 'id',
        'hide_empty'=> 0,
        'child_of' => 10, //Child From Boxes Category 
    );
    $categories = get_categories($args);
    foreach ($categories as $cat) {
        echo '<div class="one_fourth">';
        echo '<h1 class="valignmiddle uppercase title-bold">'.$cat->name.'<img src="'.$cat->term_icon.'" alt=""  class="alignleft"/>'.'<br />'.'<span class="solutions">'.$cat->description.'</span>'.'</h1>';
        $post = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 10 );
        $posts_array = get_posts( $post );

        echo '</div>';
    }
    ?>

如果还有其他方法可以获取子类别帖子并显示子类别名称和循环帖子。请让我知道这里。

If there have any other way to get child category post and display child category name and posts in loop. Please let me to know here.

推荐答案

在我解决了这个问题之后。我认为这对您有帮助。

After I solved this. I think this will help for you.

    <?php 
    $args = array(
      'orderby' => 'id',
      'hide_empty'=> 0,
      'child_of' => 5, //Child From Boxes Category 
  );
  $categories = get_categories($args);
  foreach ($categories as $cat) {
        echo '<div style="width:20%;float:left;">';
        echo '<h1 class="valignmiddle uppercase title-bold">'.$cat->name.'<img src="'.$cat->term_icon.'" alt=""  class="alignleft"/>'.'<br />'.'<span class="solutions">'.$cat->description.'</span>'.'</h1>';
        //echo '<br />';
        $args2= array("orderby"=>'name', "category" => $cat->cat_ID); // Get Post from each Sub-Category
        $posts_in_category = get_posts($args2);
        foreach($posts_in_category as $current_post) {
            echo '<span>';
            ?>
            <li type='none' style='list-style-type: none !important;'><a href="<?=$current_post->guid;?>"><?='+ '.$current_post->post_title;?></a></li>
            <?php
            echo '</span>';
        }
        echo '</div>';
    }
?>

这篇关于如何使用循环显示我的所有类别名称(WordPress)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆