WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. Roar
    Member
    Posted 9 years ago #

    Hi,

    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
    Potatoes
    Keyboard.

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

    Help?

    <li id="categories"><?php _e('News'); ?>
    <ul>
    <?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
    Member
    Posted 9 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:
    'cat=3&posts_per_page=10&what_to_show=posts'

    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(); ?>
    </a>
    </li>

    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!!!).

    -d

  3. moshu
    Member
    Posted 9 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
    Member
    Posted 9 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

Tags

No tags yet.