0

On home page i need to display few post from different categories and here are some of the issues i have

  1. Any posts can belongs to any number of categories.

this is how i am planning to do it

$cat_args = array(
     'orderby' => 'name',
      'order' => 'ASC',
     'exclude' => '17,1',
     'number' => '6'
   );

 $fcategories =   get_categories($cat_args);
    foreach($fcategories as $fcategory) {
    $post_args = array(
             posts_per_page' => 1,
             'cat' => $fcategory->cat_ID
           );

   $fposts = query_posts($post_args);
 while(have_posts()) : the_post();
 get_template_part('post', 'homepage');
 endwhile;

but i am facing few issue in this, on the post-homepage.php , i have following code to get category

$category_detail=get_the_category($post->ID);

and this can pick any category assigned to the post, moreover since same posts can be associated with multiple categories, its quite possible that same posst can appear under different category.

How can i display unique posst per category, so that no 2 category have same post. As of now i can not pass any additional parameter to get_template_part()

1

Track your post IDs.

$cat_args = array(
  'orderby' => 'name',
  'order' => 'ASC',
  'exclude' => '17,1',
  'number' => '6'
);

$fcategories = get_categories($cat_args);
$used_ids = array();
foreach($fcategories as $fcategory) {
  $post_args = array(
    'posts_per_page' => 1,
    'cat' => $fcategory->cat_ID
  );
  if (!empty($used_ids)) {
    $post_args['post__not_in'] = $used_ids;
  }

  $fposts = new WP_Query($post_args);
  if ($fposts->have_posts()) {
    while($fposts->have_posts()) {
      $fposts->the_post();
      $used_ids[] = $post->ID;
      get_template_part('post', 'homepage');
    }
  }
}

A couple of notes:

  1. There are a couple of syntax errors in your code
  2. Don't use query_posts.

    It should be noted that using this to replace the main query on a page can increase page loading times, in worst case scenarios more than doubling the amount of work needed or more. While easy to use, the function is also prone to confusion and problems later on. See the note further below on caveats for details.

    http://codex.wordpress.org/Function_Reference/query_posts (emphasis mine)

  • Tanks for the inputs.Yes i saw about code syntax as i copied and edited code for question.Thanks for giving tips about not using query_posts – Umesh Awasthi Aug 19 '13 at 5:32
  • there is one issue with the solution you provide, it seems like using new WP_Query($post_args); causing unwanted behavior and seems like it is fetching all posts of all eligible categories where as with the query_posts its working as expected – Umesh Awasthi Aug 19 '13 at 15:40
  • I can't look into this right now but query_posts is wrong. – s_ha_dum Aug 19 '13 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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