Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This question may look foolish to everyone or novice , but i am working on a theme . It will be basically a news publishing theme .

Right now , In my home page all posts are showing one after another like a usual WordPress theme.

No extra coding has been implemented on the theme .

But i want to make different sections on the home page and naming each section as its

category's title and pulling the posts from that category and show them in that section .

Like other news related themes do by making blocks in their home page and showing posts like " breaking news " , " hot right now " etc

I hope you have got the point that i am saying .

I have read about custom post types and wp_query posts but i dont have the code exactly i want for this .

Any kind of help will be very much appreciated

Gratis

share|improve this question
    
how are the categories selected? automatic from the site? from theme options? –  Michael May 17 '12 at 15:29
    
from theme option actually –  Sadi Mahmud May 17 '12 at 16:03
add comment

1 Answer 1

up vote 0 down vote accepted

You can run a foreach loop through all your categories then run a query for the latest posts in each category. You can do this by setting your home page to a static page then put this in a custom template. This is just a basic example:

$categories = get_the_categories();
    foreach ( $categories as $cat ) {

    $exclude = isset( $GLOBALS['current_id'] ) ? $GLOBALS['current_id'] : null;
    $args = array(
         'cat' => $cat->term_id,
         'posts_per_page' => 4,
         'post__not_in' => array( $exclude ),
         'no_found_rows' => true,
          );

     echo '<h3>'. $cat->name . '</h3>';
     $home_q = new WP_Query( $args );
     while ( $home_q->have_posts() ) : $home_q->the_post();
     $GLOBALS['current_id'] = get_the_ID();

     // Do loop stuff here......

     endwhile; wp_reset_postdata();

    }

The $GLOBALS['current_id'] will hold an array of post ids to use in each additional query to prevent a post from showing up more than 1 time if it's in multiple categories. Setting 'no_found_rows` to true will make the query less taxing by not getting all the posts for pagination and stuff.

share|improve this answer
    
thanks for the reply . i am trying this out now –  Sadi Mahmud May 17 '12 at 16:08
    
hey Chris_O , can i get the id from the option panel? i am using theming option framework . –  Sadi Mahmud Oct 2 '12 at 18:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.