• Hi,

    I am trying to show a list of the 10 most recent posts based on category in the sidebar.

    What I have works fine but wow it’s ugly. So I am sure there is a cleaner and better way.

    To do this I am currently using the Customizable Post Listings Plug-in:

    https://www.coffee2code.com/archives/2004/08/27/plugin-customizable-post-listings/

    And here is the current code:

    <!-- Start Custom Recent Posts - Shows Last 10 Posts Based on Category -->
    <h2>Recent Posts</h2>
    <ul>
    <?php if ( in_category(1) ) { c2c_get_recent_posts(10, "%post_URL%<br />", "1", "date", "DESC", 0);} else if (in_category (2) ) { c2c_get_recent_posts(10, "%post_URL%<br />", "2", "date", "DESC", 0); } else if (in_category (3) ) { c2c_get_recent_posts(10, "%post_URL%<br />", "3", "date", "DESC", 0); } else if (in_category (4) ) { c2c_get_recent_posts(10, "%post_URL%<br />", "4", "date", "DESC", 0); } else if (in_category (5) ) { c2c_get_recent_posts(10, "%post_URL%<br />", "5", "date", "DESC", 0); } else if (in_category (6) ) { c2c_get_recent_posts(10, "%post_URL%<br />", "6", "date", "DESC", 0); } else if (in_category (7) ) { c2c_get_recent_posts(10, "%post_URL%<br />", "7", "date", "DESC", 0); } else if (in_category (8) ) { c2c_get_recent_posts(10, "%post_URL%<br />", "8", "date", "DESC", 0); } else if (in_category (9) ) { c2c_get_recent_posts(10, "%post_URL%<br />", "9", "date", "DESC", 0); } else if (in_category (10) ) { c2c_get_recent_posts(10, "%post_URL%<br />", "10", "date", "DESC", 0); } else if (in_category (11) ) { c2c_get_recent_posts(10, "%post_URL%<br />", "11", "date", "DESC", 0); } else { echo 'Coming Soon';} ?>
    </ul>
    <!-- End Custom Recent Posts -->

    Any ideas on how to clean it up a bit?

    Thanks,

    Will

Viewing 15 replies - 1 through 15 (of 16 total)
  • <?php c2c_get_recent_posts(10, "%post_URL%<br />", "$cat", "date", "DESC", 0); ?>

    The $cat variable is a global var when on a category query.

    Thread Starter war59312

    (@war59312)

    Hi,

    Awesome! Had no idea it was that simple. Thanks a lot!

    What about adding dates in the mix?

    That is only display posts for a certain month and year for the current category. Like for archives?

    Thanks again,

    Will

    What about adding dates in the mix?

    Does the plugin you’re using offer such a limitation? I’ve no idea, but you could look at replacing the plugin function you’re calling with query_posts(). You’ll see information there on category and “time” parameters.

    Thread Starter war59312

    (@war59312)

    Hey,

    Thanks Kafkaesqui, but it only seems to work in the loop. Trying to get it working on the sidebar instead… Any ideas?

    Thanks again,

    Will

    WPChina

    (@wordpresschina)

    Ok, I have another problem I’m trying to solve with this plugin (or maybe I need another??).

    I want to only show the last 7 days of posts within the “category” using c2c_get_recent_posts. Is there a way to only show the posts from the past 7 days–maybe there are zero or maybe there are dozens of posts, but it will only show that week’s worth of posts?

    Tks for your help!

    WPChina

    (@wordpresschina)

    Ok I’m trying to put this before my loop, but it’s not returning anything:

    <?php query_posts('cat=3&hour=168&orderby=date&order=DESC'); ?>

    I did hour=168, because I only want to show the last 7 days, and there are 168 hours in 7 days… this is not working… any ideas??

    WPChina

    (@wordpresschina)

    big headache…. after looking at the codex more and more, I am changing my approach and I ALMOST have it, but something is still missing. I’m now putting this before the loop:

    <?php $current_month = date('m'); ?>
    <?php $current_year = date('Y'); ?>
    <?php query_posts("cat=8&year=$current_year&monthnum=$current_month&order=DESC"); ?>

    Now the above returns all the posts from the current month… I want to only return from the current week…. I have not figured out how to get there… anybody have ideas?

    WPChina

    (@wordpresschina)

    any ideas how to get this to work?

    WPChina

    (@wordpresschina)

    I try using current_week, $week, and a few other similar things but not work. Anyone know how to show this as only the present 7 days?

    WPChina

    (@wordpresschina)

    In past versions of WordPress, it was possible to limit posts on the front page to only the last X days. This was removed a couple months ago… this is something like what I need!

    Does anyone know how to only show the last X days? The codex seems to not have this info…

    Psuedo code:

    1. Convert current date to a julian date and subtract 7
    2. Get the posts with query_posts
    3. Using The Loop to loop through the each post
    3a. Convert the post date to a julian date
    3b. If the post julian date is less than the value from step 1, ignore it and loop to next post otherwise display that post

    How does that sound?

    WPChina

    (@wordpresschina)

    Thank you thank you for your message and it sounds great… ??

    But my problem is though I can look at code and understand what is happening, I haven’t reached the point of crafting my own based on your ideas. Does anyone have examples of this working?

    Then you might want to look at the Custom Query String plugin.

    https://drunkenmonkey.org/user/archive/custom-query-string-2.6.zip

    WPChina

    (@wordpresschina)

    Thank you for the advice–I am still pulling my hair out over this! Arrrgg…

    I’ve been using CQS for a long time and didn’t realize what you just said–I can show by date!! That’s great! I’m glad I took another look at it ??

    However, it seems it only output a certain number of days of posts within a category, but doesn’t work if I place a list of categories and their posts on the same page, right?

    One one page I want to do:

    Category 1
    Post 1.1
    Post 1.2
    Post 1.3

    Category 2
    Post 2.1
    Post 2.2
    Post 2.3

    etc…

    I want to create a separate page for this and then do this… I will experiment in a few ways and post my results soon.

    WPChina

    (@wordpresschina)

    Still no luck MichaelH. The Customizable Post Listings Plug-in is fantastic and would almost do what I need it to do, only it does not allow me to limit posts to only the last X days ??

    Still looking for a solution….

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Show Recent Posts Based on Category’ is closed to new replies.