• Resolved chudsf

    (@chudsf)


    On the index page of the blog, I would like to ONLY display the category of the most recent post. So if I post category 6, it should only display posts of that category. If I then do a post of category 3, it would no longer show category 6, only category 3. And so on…

    Basically the blog will have a rotating identity on the index page (I have a different header image for each category too), and this will keep older/different category posts from showing up there.

    Anyone know a way to do this? Asked a month ago, thought I’d try again.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hmm. I guess you could perform a quick SQL query for it:

    <?php $latest_cat = $wpdb->get_var("SELECT category_id FROM $wpdb->posts LEFT JOIN $wpdb->post2cat ON ID = post_id WHERE post_status = 'publish' ORDER BY post_date DESC LIMIT 1"); ?>

    Then use query_posts() to initialize The Loop in your template, passing $latest_cat for the value of ‘cat’:

    <?php query_posts("cat=$latest_cat"); ?>

    Thread Starter chudsf

    (@chudsf)

    I will give this a shot, thank you so much!

    Thread Starter chudsf

    (@chudsf)

    This worked swimmingly with one exception — If I schedule posts in advance it grabs the $latest_cat value from those (instead of the current, displayed post). Anyone care to suggest a way to filter it so it only grabs the latest LIVE post?

    Change the query to:

    <?php
    $now = current_time('mysql');
    $latest_cat = $wpdb->get_var("SELECT category_id FROM $wpdb->posts LEFT JOIN $wpdb->post2cat ON ID = post_id WHERE post_status = 'publish' AND post_date <= '$now' ORDER BY post_date DESC LIMIT 1");
    ?>

    For WordPress 2.1, this should do:

    <?php
    $latest_cat = $wpdb->get_var("SELECT category_id FROM $wpdb->posts LEFT JOIN $wpdb->post2cat ON ID = post_id WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date DESC LIMIT 1");
    ?>

    Thread Starter chudsf

    (@chudsf)

    Excellent. Thanks so much, Kafkaesqui, your assistance is much appreciated.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Need to display ONLY the category of the current top post’ is closed to new replies.