WordPress.org

Ready to get started?Download WordPress

Forums

Show Limited Number of Posts in Sepcific Category on Home (6 posts)

  1. MikeCherim
    Member
    Posted 7 years ago #

    Hello all,

    I want to show a specific number of post title links on the index to a single category.

    I have had success with showing the linked titles to a specific category using this:

    <?php if ( is_home()) { ?>
    <?php if(in_category(2)) : ?>
    <h2>Check Out Recent Posts</h2>
    <?php while (have_posts()) : the_post(); ?>
    <ul>
    <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Read: <?php the_title(); ?>"><?php the_title(); ?></a></li>
    </ul>
    <?php endwhile; ?>
    <?php endif; ?>
    <?php } ?>

    And I can limit the numbers with this:

    <?php if ( is_home()) { ?>
    <h2>Check Out Recent Posts</h2>
    <ul><?php get_archives('postbypost','4','custom','<li>','</li>'); ?></ul>
    <?php } ?>

    But I've had no success combining the two, say showing the linked title to the 4 most recent posts in category 2 on the index.

    Can someone help please?

    Mike

  2. Chris_K
    Member
    Posted 7 years ago #

  3. MikeCherim
    Member
    Posted 7 years ago #

    Hello HandySolo,

    I think that may be a lot more than I need actually. If I can either...

    1) Limit the number of post titles shown in my first code block by entering some number-of-posts parameter or;

    2) Define only post titles from a specific category with my second code block;

    ...I'd have exactly what I need. The solution does not need to be cross theme compatible so it'll be hard-coded in the index.php.

    The goal, for example, is to show 4 of the most recent post titles from category 2 (again, as an example). I'm so close to the solution I can taste it, I'm just missing something small.

    Mike

  4. moshu
    Member
    Posted 7 years ago #

    Re 2) - get_archives does NOT have a category parameter - so, it's not the right tool for what you want.

    You may want to check out this template tag:
    http://codex.wordpress.org/Template_Tags/query_posts

  5. MikeCherim
    Member
    Posted 7 years ago #

    Hello Moshu,

    I think that's probably what I want so I can pass the parameters I need. But, I can't seem to get it to work:

    I tried several things, but I can't seem to limit the number of posts differently than what I have in my configured for my index.

    <?php if ( is_home() ) { ?>
    <h2>Check Out Recent Posts</h2>
    <?php query_posts("cat=2,showposts=2"); ?>
    <?php while (have_posts()) : the_post(); ?>
    <ul>
    <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Read: <?php the_title(); ?>"><?php the_title(); ?></a></li>
    </ul>
    <?php endwhile; ?>
    <?php } ?>

    The query_posts string is different than the examples on that page, but I tried in that way too. In any case the results seem to be the same. This isn't too bad, I mean it is a good solution, unless the number of posts to show on home and the number of links to show for the specific category is different.

    Any idea why "showposts=" isn't working?

    Mike

  6. MikeCherim
    Member
    Posted 7 years ago #

    Okay, I came up with a completely different solution, one that meets all of my requirements. I will post the solution below and show this as resolved in case someone else needs this:

    In the scripting below the category is "Reviews" as in movie reviews and is category number 2. I will show the 5 most recent review titles in descending order (default).

    <?php if ( is_home()) { ?>
    <h2>Read Our Recent Reviews</h2>
    <ul>
    <?php
    $myposts = get_posts('numberposts=5&category=2');
    foreach($myposts as $post) :
    ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>
    <?php } ?>

    In the code block below I will add an offset to skip the most recent review since it'll show up at the top of the index anyway.

    <?php if ( is_home()) { ?>
    <h2>Read Our Recent Reviews</h2>
    <ul>
    <?php
    $myposts = get_posts('numberposts=5&offset=1&category=2');
    foreach($myposts as $post) :
    ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>
    <?php } ?>

    Tah-dah! Thanks for everyone who helped.

    Mike

Topic Closed

This topic has been closed to new replies.

About this Topic