Forums

[resolved] Latest post from each category (4 posts)

  1. Karkof
    Member
    Posted 2 years ago #

    Hey,

    I'm new to WordPress and I'm trying to get the latest posts from three different categories on the index page. This is my first time getting stuck into PHP as well so any help is appreciated.

    I've tried searching for threads addressing this, but I've only been able to come up with ways to show a list of all the posts in any category. I understand it's quite a small step from where I am at the moment to my goal so any help would be apreciated.

    Here is my code at the mo:

    <?php if(have_posts()) : while(have_posts()) : the_post(); ?>
        <? if ($post->ID != $featured_ID) { ?>
    	<div class="mini_block">
    		<img src="<?php bloginfo('template_directory'); ?>/images/TransitionYear.png" class="mini_ribbon" alt="Transition Year" />
    		<div class="block_inside">
    			 <?php the_post_thumbnail(); ?>
    				<div class="mini_text_block">
    					<h3><a href="<?php the_permalink(); ?>"title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
    					<small>on <?php the_time('M d'); ?> in <?php the_category(', '); ?> tagged <?php the_tags(''); ?> by <?php the_author_posts_link(); ?></small>
    					<?php the_excerpt('Read More'); ?>
    					<div class="separator biggap"></div>
    				</div>
    			</div>
    		</div>
    	<? } ?>
    	<?php endwhile ?>
    	<div id="posts_navigation">
    		<?php previous_posts_link(); ?>
    		<?php next_posts_link(); ?>
    	 </div>
    		<?php else : ?>
    		<h2 class="center">Not Found</h2>
    		 <p class="center">Sorry, but you are looking for something that isn’t here.</p>
    		 <?php include (TEMPLATEPATH . '/searchform.php'); ?>
    <?php endif; ?>

    Let me know if I'm going about this the completly wrong way.

  2. MichaelH
    Volunteer
    Posted 2 years ago #

    <?php
    //get lastest post in each of 3 categories (1,2,3)
    $taxonomy = 'category';//  e.g. post_tag, category
    $param_type = 'category__in'; //  e.g. tag__in, category__in
    $term_args=array(
      'include' => '1,2,3',
      'orderby' => 'name',
      'order' => 'ASC'
    );
    $terms = get_terms($taxonomy,$term_args);
    if ($terms) {
      foreach( $terms as $term ) {
        $args=array(
          "$param_type" => array($term->term_id),
          'post_type' => 'post',
          'post_status' => 'publish',
          'posts_per_page' => 1,
          'caller_get_posts'=> 1
          );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo 'List of Posts in '.$taxonomy .' '.$term->name;
          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. Karkof
    Member
    Posted 2 years ago #

    Oh wow. Thanks.

  4. aminabbasian
    Member
    Posted 1 year ago #

    Michael,

    How can I get this to work on the following:

    http://wordpress.org/support/topic/link-to-latest-post-by-category

    how I'd like the links set out is shown here: http://wordpress.pastebin.com/icrXJzda.

    Thanks

Topic Closed

This topic has been closed to new replies.

About this Topic