ThemeShaper Forums » Thematic

[closed]

Showing 5 latest blog posts

(6 posts)
  • Started 8 years ago by NiveouS
  • Latest reply from helgatheviking
  • This topic is not resolved
  1. NiveouS
    Member

    Just wondering if there is a more simple way to show the 5 most recent blog posts on the home page, using the Thematic theme layout of displaying them?

    Normally I use:

    <?php
    $lastposts = get_posts('numberposts=5');
    foreach($lastposts as $post) :
    setup_postdata($post);
    ?>
    
    <div class="postItem">
    <h2><a>"><?php the_title(); ?></a></h2>
    <div class="meta">
    <?php the_time('M j, Y') ?> // by <span class="author"><?php the_author_link(); ?></span> //  <?php the_category(', ') ?> // <?php comments_popup_link('No Comments', '1 Comment ', '% Comments'); ?>
    </div><!-- #meta -->
    
     <?php the_content(); ?>
    
     </div><!-- #post-item-->
    
     <?php endforeach; ?>

    Just wondering if there is a simpler way - since I seem to be needing the snippet more and more often. Any feature on Thematic?

    Posted 8 years ago #
  2. I use the following. I believe it's better to use WP_Query instead of get_posts.

    <ul>
    <?php
     $recentPosts = new WP_Query();
     $recentPosts->query('showposts=5');
    ?>
    <?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
     <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    </ul>
    Posted 8 years ago #
  3. I agree with using the new WP_Query method. I never have loop issues or reset issues when using it.

    One note though, showposts=5, is deprecated. To avoid future possible issues, one should use posts_per_page=5.

    Posted 8 years ago #
  4. interesting. i always use get_posts(). is your home page a static page you need to add stuff to or is it the traditional blog page and you just want to limit the posts to 5?

    Posted 8 years ago #
  5. new WP_Query() seems to me to be so much better documented. I tried get_posts() once and found the codex to be lacking docs and examples. :)

    Posted 8 years ago #
  6. to each his/her own- there is more than 1 way to skin a cat. and if you ask me, as long as it works it is the right way. but this example in the codex should be enough to set up any kind of loop.

    <?php
    $args = array( 'numberposts' => 3 );
    $lastposts = get_posts( $args );
    foreach($lastposts as $post) : setup_postdata($post); ?>
    	<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    	<?php the_content(); ?>
    <?php endforeach; ?>
    Posted 8 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.