• Resolved Andrew

    (@andrew)


    Hey everyone,

    I’m working on this one WordPress site. This site in particular has a category entitled “Book and Movie Reviews.”

    I need to list the last five or so posts in this category�just like the get_archives tag, except only for a specific category. Can this be done? Are there any PHP wizards out there?

    Thanks!

    �Andrew

Viewing 12 replies - 1 through 12 (of 12 total)
  • You may want to take a look at the query_posts tag:
    Template_Tags/query_posts

    Thread Starter Andrew

    (@andrew)

    Hmm. Unfortunately, the query_posts tag isn’t going to work in this case. I need it to do exactly what this tag does: get_archives('postbypost', '5', 'html') EXCEPT I need it to display the post archives only for a certain category. Whereas the query_post tag gets the actual post data, the get_archives tag retrieves posts’ titles (which then have links to their respective single page). I need something like the get_archives tag.

    The site I’m working on has a two-column design, and I need to fit this in the right column (in other words, it needs to be in a listâ€â€?again, like the get_archives tag).

    I am afraid you are wrong. I remember using this code for a client of mine:
    <?php $temp_query = $wp_query; ?>
    <?php query_posts('cat=1&showposts=5'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <h5 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h5>
    <?php endwhile; ?>
    <?php $wp_query = $temp_query; ?>

    and it did exactly what you want. Of course, you can style it differently (replace the h5, etc.), but it should work.

    Thread Starter Andrew

    (@andrew)

    Moshu: That worked perfectly. Thank you very, very much!

    I added this loop to my homepage to show the last 10 posts of a particular category at the top of my homepage, and left in the regular home page post loop to show all the posts.

    Now my pagination has disappeared from the bottom.. Any clues?

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Generally speaking, query_posts() is the wrong way to do this, because it may affect other parts of the page, due to the way the posts system works.

    The better way is with get_posts(). The difference is that get_posts returns the posts immediately, whereas query_posts sets up the query for the have_posts and the_post functions.

    See the examples here: http://codex.wordpress.org/Template_Tags/get_posts

      <?php
      $posts = get_posts(‘numberposts=10&category=76’);
      foreach($posts as $post) :
      ?>

    • “><?php the_title(); ?> — <?php the_excerpt(); ?>
    • <?php endforeach; ?>

    <br><br>

    I tried add the example.. It listed the 10 posts, but I got errors in the for all the other posts on the page in the general loop. see below:

    WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘AND comment_approved = ‘1” at line 1]
    SELECT COUNT(comment_ID) FROM blog_wp_comments WHERE comment_post_ID = AND comment_approved = ‘1’;

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Hmmm… Try adding a query_posts(); call right before your main loop. No parameters to it, go with the defaults.

    Thanks otto for all your help.. I put in the call right before the main loop like this:

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

    <div class=”post” id=”post-<?php the_ID(); ?>”>
    <h3 class=”storytitle”>” rel=”bookmark”><?php the_title(); ?></h3>
    <div class=”expiration”>
    <?php if (has_expiration_date()): ?>
    <?php if (has_expired()): ?>
    <br/><b><font color=”blue”>This offer has expired</font></b>
    <?php else: ?>

    <?php endif; ?>
    <?php endif; ?>
    </div>

    and I still get this error:

    WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘AND comment_approved = ‘1” at line 1]
    SELECT COUNT(comment_ID) FROM blog_wp_comments WHERE comment_post_ID = AND comment_approved = ‘1’;

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Well, I have no idea then. That’s just weird. Try going back to the query_posts method, but leave the new query_posts() call in there and see if that fixes the lack of navigation links problem.

    csbaff, I’m having the same problem you are with the call right before the main loop. Did you get this sorted?

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘List last 5 posts of a certain category? Help, please!’ is closed to new replies.