• Resolved kamran210

    (@kamran210)


    Hi ,
    I am trying to show posts grouped under categories in index.php with no success .

    What i am trying to do :
    ———————————–
    Parent Category Name

      5 Post titles in this category

    ————————————
    Parent Category Name 2

      5 Post titles in this category2

    ————————————
    for all parent categories.

    One option that i found is this ;
    `<div>
    <p>cat1</p>
    <?php $my_query = new WP_Query( ‘category_name=cat1&posts_per_page=7’ );
    while ( $my_query->have_posts() ) : $my_query->the_post();
    $do_not_duplicate = $post->ID; ?>
    <h3 ><a href=”<?php the_permalink(); ?>”><?php the_title();?></a></h3>
    <?php endwhile; ?>
    </div>

    But if i use this then i will have to repeat same code for all categories and then also for child categories in category template.

    . I am stuck in this .
    Help would be really really appreciated .

Viewing 1 replies (of 1 total)
  • Thread Starter kamran210

    (@kamran210)

    Solution:

    <?php
    
    //for each category, show 1 post
    $cat_args=array(
      'orderby' => 'name',
      'order' => 'ASC'
       );
    $categories=get_categories($cat_args);
      foreach($categories as $category) {
        $args=array(
          'showposts' => 5,
          'category__in' => array($category->term_id),
          'caller_get_posts'=>5
        );
        $posts=get_posts($args);
          if ($posts && $category->category_parent == 0) {
           ?>
    <div style="border:thick solid #0000FF; ">
      <?php
            echo 'Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> ';
            foreach($posts as $post) {
              setup_postdata($post); ?>
      <p>
        <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?>
        </a>
      </p>
      <?php
            } // foreach($posts
                  ?>
    </div>
    <?php
          } // if ($posts
    
        } // foreach($categories
    ?>

Viewing 1 replies (of 1 total)
  • The topic ‘grouping posts under same categories on homepage’ is closed to new replies.