• Hi guys,
    I am trying to output a list of recent posts, but would like to style each list item depending on the category the post is saved in.

    IE, the list output would look something like this:

    <ul id="myposts">
    <li class="cat-books"><a href="#" title="post title">post title</a>
    <li class="cat-movies"><a href="#" title="post title">post title</a>
    <li class="cat-books"><a href="#" title="post title">post title</a>
    <li class="cat-music"><a href="#" title="post title">post title</a>
    <li class="cat-movies"><a href="#" title="post title">post title</a>
    </ul>

    This is the code that i am trying:

    <?php query_posts('showposts=10'); ?>
    <ul id="myposts">
      <?php while (have_posts()) : the_post(); ?>
      <li class="cat-<?php $posts->cat_name; ?>"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
      <?php endwhile;?>
    </ul>

    I realize it was a long shot and it makes sense why its not working, since i havent added any code to retrieve anything from the terms table in the db.

    Anyone fancy pointing me in the right direction?

    Cheers,
    Chris

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Chris Rault

    (@connectr)

    I managed to get this working (to some extent) using this plugin – https://www.dagondesign.com/articles/latest-post-from-each-category-plugin-for-wordpress/. I would still prefer the normal latest posts list as in my first example (as it serves as a nice clear way to see what kind of activity has taken place on the site). If anyone has any pointers i would really appreciate it ??

    Cheers,
    Chris

    Heya Chris ??

    Here’s how its done:

    <ul>
    <? $listCats = new WP_Query(); $listCats->query('showposts=10'); while ($listCats->have_posts()) : $listCats->the_post(); ?>
    <li><a href="<? the_permalink(); ?>" title="<? the_title(); ?>" class="<?php $category = get_the_category(); echo $category[0]->category_nicename ; ?>"><? the_title(); ?></a></li>
    <? endwhile; ?>
    </ul>

    Enjoy ??

    Thread Starter Chris Rault

    (@connectr)

    Awesome dude! That works like a charm!!!! I adjusted it a wee bit to apply the class on the LI instead of the A to give a bit more control, but apart from that its super sweet ?? This will definitely come in handy!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Latest posts list with category id as a class on list items’ is closed to new replies.