Support » Fixing WordPress » last post from each category, impossible?

  • I’d been trying to get the last post from each category in a “page”, and even if I were able to make 2 different loops on the same page (1 for loading the requested page and the other for the blogs from each cat) I couldn’t make the neccesary loop for cycling thru the cats and read the last post.

    I’ve got some help from mores but the script didn’t work. It just reads the requested page, even removing the first loop and leaving the cats one.

    The test page is at here
    and should replace this current messy version (Blogs show up when clicking the “Articles” tab below the flash header.

    <?php /*
    Template Name: FirstPostOfEachCategory
    */ ?>
    <?php get_header(); ?>
    <div id="content">
    <div id="main">
    	<?php // Here we output the actual content of the Page. If you need introductory text or something
    		if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    	<h1><?php the_title(); ?></h1>
    		<?php the_content(); ?>
    	<?php endwhile; else: endif; ?>
    	<?php // this is where the categories get cycled through and the latest X posts of each are displayed
    	$categories = $wpdb->get_results("SELECT $wpdb->terms.term_id AS id, name, description, term_order from $wpdb->terms INNER JOIN $wpdb->term_taxonomy ON $wpdb->terms.term_id = $wpdb->term_taxonomy.term_id WHERE taxonomy = 'category' AND parent = 0 ORDER BY term_order ASC");
    	foreach ($categories as $category) { ?>
    		<h1>Category: <a href="<?php echo get_category_link($category->id); ?>"><?php echo $category->name; ?></a></h1>
    		<?php query_posts('category_name='.$category->name.'&showposts=1'); } ?>
    		<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    			<?php the_title(); ?>
    			<!-- Style this any way you want -->
    		<?php endwhile; else: ?><p>Nix!</p><?php endif; ?>
    	<?php }	?>
    <?php get_footer(); ?>
Viewing 5 replies - 1 through 5 (of 5 total)
  • Try this instead:

    <?php // cycle through categories, print 1 post for each category
      foreach($categories as $category) {
          $posts=get_posts('showposts=1&cat='. $category->term_id);
          if ($posts) {
            echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
            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>
            } // foreach($posts
          } // if ($posts
        } // foreach($categories

    I have 5 Category but i want for add code at index just for 2 category.

    Last 5 post USA
    Last 5 post Europe


    Then use the get_categories include= argument such as this for categories 7 and 8


    Stepping Into Template Tags
    Stepping Into Templates
    Template Hierarchy

    I ask for Last Post By Category with thumbnail in display.

    Demo :

    And i see at this themee is free for download, but is not into plugin for Last Post By Category with thumbnail in display

    Latest Articles On Business

    Latest Articles On Cell Phones

    Latest Articles On Entertainment

    I search this plugin for download free.
    I hope to help me.
    Thank’s for all.


Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘last post from each category, impossible?’ is closed to new replies.