WordPress.org

Ready to get started?Download WordPress

Forums

last post from each category, impossible? (6 posts)

  1. Zambrano Sergio
    Member
    Posted 5 years ago #

    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; ?>
    	</div>
    	<?php }	?>
    
    </div>
    </div>
    
    <?php get_footer(); ?>
  2. MichaelH
    Member
    Posted 5 years ago #

    Try this instead:

    <?php // cycle through categories, print 1 post for each category
    $categories=get_categories('orderby=name&order=ASC');
      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>
              <?php
            } // foreach($posts
          } // if ($posts
        } // foreach($categories
    ?>
  3. suzanb
    Member
    Posted 4 years ago #

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

    Last 5 post USA
    Last 5 post Europe

    Example:
    USA, EUROPA, MICROSOFT, GOOGLE, TEST

  4. MichaelH
    Member
    Posted 4 years ago #

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

    $categories=get_categories('include=7,8&orderby=name&order=ASC');

    Related:
    Stepping Into Template Tags
    Stepping Into Templates
    Template Hierarchy

  5. suzanb
    Member
    Posted 4 years ago #

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

    http://www.freeimagehosting.net/image.php?929e336b7c.jpg

    Demo : http://demo.magpress.com/index.php?wptheme=Bluevilla

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

    Category:
    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.

  6. suzanb
    Member
    Posted 4 years ago #

    ?

Topic Closed

This topic has been closed to new replies.

About this Topic