Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display my all category names using loop (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.

like image 269
yeshansachithak Avatar asked Nov 29 '22 15:11

yeshansachithak


1 Answers

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>';
    }
?>
like image 146
yeshansachithak Avatar answered Dec 06 '22 03:12

yeshansachithak