The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

Help fix code for Recent Posts by Category? (4 posts)

  1. Roar
    Posted 11 years ago #


    This is a two part question.

    The following code is doing what I want as far as returning recent post titles limited to one category.

    1. I would like to be able to limit the number of entries it returns to 10 or so.

    Secondly, it is showing them with only a space between them, i.e.,:
    June bugs, potatoes, keyboard

    I would like it to look like:
    June Bugs

    However, clearly, I am too daft to figure it out.


    <li id="categories"><?php _e('News'); ?>
    <?php $temp_query = $wp_query; ?>
    <?php query_posts('cat=3'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <id="post-<?php the_ID(); ?>">
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
    <?php the_title(); ?></a><br>
    <?php endwhile; ?>
    <?php $wp_query = $temp_query; ?>

  2. davidchait
    Posted 11 years ago #

    CG-SameCat already sort of does what you're looking for, without putting a heavy load on the server. Without a custom query, I believe you are actually pulling the entirety of all posts returned by the search (that is, the entire post_content block!), when you just really just need the id, title, and date maybe. Certainly not the content. ;)

    But, if you really want to go this approach, here's your two best solutions:

    1. limit it on the query, not on the output, else you're pulling in every post from category 3 (which depending on your site could be 5, or 500!). Looking at the query logic, you want to pass a few more args to the query. You can do it via the query_posts string, or by the $wp_query->set() function. I'll use the query string for the moment:

    I think that'll get you your 10 posts.

    2. try using the following inside the while block:

    <li id="sidebar-post-<?php the_ID(); ?>">
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
    <?php the_title(); ?>

    note the li wrappering of each item, and the id name being specific to the sidebar list (as the post could have shown already in the main loop, in which case id "post-217" or something might already exist on the page!!!).


  3. moshu
    Posted 11 years ago #

    1. One of the PHP gurus (Kafkaesquí) gave me this code:

    <?php $top_query = new WP_Query('cat=1&showposts=1'); ?>
    <?php while($top_query->have_posts()) : $top_query->the_post(); ?>

    ~ template tags 'n stuff go here ~

    <?php endwhile; ?>

    As you can see ti's possible to define/limit the number of posts shown.
    2. I guess that's a pure CSS thing: put it in a div, class, something and style it through css.

  4. Roar
    Posted 11 years ago #

    Ah, well here is the dilemma... I want to be able to do this for four different categories. Let me do some searching on the CG-SameCat, as I have not heard of that one.

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.