• Hi,

    I would like to show my 5 latest posts on footer (archive).
    These 5 latest posts come from 2 different categories (journal / note)

    I would like to style my post so that posts from journal category have class of “journal” while posts from note category have class of “note”.

    Is there any specific conditional query for this?

    Thanks!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Use something like <div <?php post_class();?>> and WP will automatically create classes that include the post’s categories.

    Thread Starter ivanasetiawan

    (@ivanasetiawan)

    hi!

    Thanks for the reply.
    the code works perfect but that’s not exactly what I need.

    As for now, I use this to show my latest 10 posts:
    <?php wp_get_archives(‘title_li=&type=postbypost&limit=10’); ?>

    this code automatically generate something like this:

    <ul id="blahblah">
    <li><a href="">post three from "A" category</a></li>
    <li><a href="">post two from "B" category</a></li>
    <li><a href="">post one from "A" category</a></li>
    </ul>

    And what I wanna do is:

    <ul id="blahblah">
      <li class="post_from_a_category"><a href="">post three from "A" category</a>
      <li class="post_from_b_category"><a href="">post two from "B" category</a>
      <li class="post_from_a_category"><a href="">post one from "A" category</a>
    </ul>

    so posts from “A” category will have its own class name – while posts from “B” category also have its own class name (so I can style ’em)

    can you please help me with this?

    You’re not going to be able to achieve this using wp_get_archives. You might be better of using get_posts:

    <ul id="blahblah">
    <?php
    global $post;
    $myposts = get_posts('numberposts=10');
    foreach($myposts as $post) :
    setup_postdata($post);
    $cat_name = '';
    $category = get_the_category();
    $cat_name = $category[0]->cat_name;
    ?>
    <li class="<?php echo $cat_name;?>"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>
    Thread Starter ivanasetiawan

    (@ivanasetiawan)

    Wow!! Thanks a lot, Esmi!
    You’re da best ??
    I changed the cat_name to category_nicename (personal preferences tho)

    off the topic – I tried to grab my latest twitter update by using this:

    <?php function
    			getTwitterStatus($userid){$url = "https://twitter.com/statuses/user_timeline/$userid.xml?count=1";$xml = simplexml_load_file($url) or die("Oopsie!");
    			foreach($xml->status as $status){$text = $status->text;}
                echo $text;}
                //my user id ivanasetiawan
                getTwitterStatus("ivanasetiawan");
    ?>

    it works out just fine on other sites – but on my site I see these warnings:

    Warning: simplexml_load_file() [function.simplexml-load-file]: URL file-access is disabled in the server configuration in /home/ivanaset/public_html/wp-content/themes/starkers-html5/index.php on line 187
    
    Warning: simplexml_load_file(https://twitter.com/statuses/user_timeline/ivanasetiawan.xml?count=1) [function.simplexml-load-file]: failed to open stream: no suitable wrapper could be found in /home/ivanaset/public_html/wp-content/themes/starkers-html5/index.php on line 187
    
    Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "https://twitter.com/statuses/user_timeline/ivanasetiawan.xml?count=1" in /home/ivanaset/public_html/wp-content/themes/starkers-html5/index.php on line 187
    Oopsie!

    Any ideas? ??

    Looks like the server is locked down a little tightly. It’s refusing to allow the theme to access an external(?) file via URL.

    Thread Starter ivanasetiawan

    (@ivanasetiawan)

    all set up ??

    One minor question – Is it possible to exclude a certain category from the latest posts?

    Thanks ??

    If you’re using get_posts, you could use something like $myposts = get_posts('numberposts=10&cat=-4'); to exclude all posts from Category ID 4.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘how to give different class names, based on category, on latest post?’ is closed to new replies.