Forums

How can I display two most recent posts per category on the home page? (7 posts)

  1. PowerPro
    Member
    Posted 1 year ago #

    My question is similar to this one...

    http://wordpress.org/support/topic/how-can-i-display-two-posts-per-category-on-the-home-page?replies=4

    ...except I want it to include all categories and I don't want it in a specific category order...rather I just want the two latest posts from each category.

    BTW...I'm starting this new thread because the other says "resolved".

    Help please!

  2. t-p
    Member
    Posted 1 year ago #

    the following code will display 2 posts from a category. See if you can modify it to include 2 posts per category:

    <?php
    //display 2 posts for category id 47
        $args=array(
          'cat' => 47,
          'post_type' => 'post',
          'post_status' => 'publish',
          'posts_per_page' => 2,
          'caller_get_posts'=> 1
          );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo 'List of Posts';
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
           <?php
          endwhile;
        }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
  3. PowerPro
    Member
    Posted 1 year ago #

    Oh also... I need to ensure that posts only show up one time even if they're in multiple categories.

    Here's how it looks right now (you'll note that I've set it so that titles of posts from category 40 are red). This is what I need to modify:

    <?php if (have_posts()) : ?>
                <?php while (have_posts()) : the_post(); ?> 
    
                <div class="galleryitem" id="post-<?php the_ID(); ?>">
                    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><img src="<?php if ( function_exists('p75GetThumbnail') )echo p75GetThumbnail($post->ID); ?>" alt="<?php the_title(); ?>" /></a>
    
    <!--custom color for post title by category -->
    
    <?php if (in_category(40) ) { ?>
    <h3><a style="color:#990000; font-weight: bold;" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title2('', '...', true, '20') ?></a></h3>
    <? } else { ?>
    <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title2('', '...', true, '20') ?></a></h3>
    <? } ?>
    
    <!-- end custom color for post title -->
    
                    <div class="commentsnumber">
                        <a href="<?php the_permalink() ?>#comments" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php comments_number('0', '1', '%'); ?></a>
                    </div> 
    
                    <p><?php the_content_rss('', TRUE, '', 25); ?></p>
    <p style="margin:12px 0 0 5px;">
    <?php _e('Posted in:'); ?> <?php the_category(', ') ?></p> 
    
                </div>
    
                <?php endwhile; ?>
  4. alchymyth
    The Sweeper
    Posted 1 year ago #

    a:
    to show 2 posts of all categories:
    try to change this line (in the code sugested in http://wordpress.org/support/topic/how-can-i-display-two-posts-per-category-on-the-home-page?replies=4 ):
    foreach( array(4, 3, 1) as $cat_id ) {
    to:
    foreach( get_categories() as $cat_id ) {
    http://codex.wordpress.org/Function_Reference/get_categories

    b:
    methods for avoiding double posts are in:
    http://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action
    (read all, but particular the array method described after 'Note for Multiple Posts in the First Category')

    c:
    for any help, please paste the full template into a http://pastebin.com/ and post the link to it here.

  5. t-p
    Member
    Posted 1 year ago #

    Or, try suggestion of this thread

  6. PowerPro
    Member
    Posted 1 year ago #

    Alright... after tweaking the first part of the answer to include all categories, it looks like this:

    [next time, please use the pastebin for any code longer than 10 lines]

    <?php
    foreach( get_categories() as $cat_id ) {
      $args=array(
        'cat' => $cat_id,
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => 2,
        'caller_get_posts'=> 1
      );
      $my_query = null;
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        echo 'List of Posts for category '. $cat_id;
        while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate[] = $post->ID ?>
    
    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
    
          <?php
        //the_content();  //or the_excerpt{};
        endwhile;
      }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    But in the second part of the answer:

    $my_query = new WP_Query('category_name=featured&posts_per_page=2');

    How do I blend them?

    Also... am I reading this right in thinking that this part:

    <?php if (have_posts()) : ?>
                <?php while (have_posts()) : the_post(); ?>

    ...is what I'm replacing with the final version of the above?

    I'm a code tweaker and not a coder thus all the questions. ;)

    Thank you!

  7. teckn1caLity
    Member
    Posted 9 months ago #

    I actually need the exact same thing PowerPro is looking for but don't understand the code being posted.

Topic Closed

This topic has been closed to new replies.

About this Topic