Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have this function which outputs the last 6 posts of categories 1 and 2.

<ul>
<?php query_posts('cat=1,2&showposts=6'); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><strong><?php the_title(); ?></strong></a><br />
    <em><?php echo substr(get_the_excerpt(), 0,110); ?> ...</em>
    </li>
    <br />
<?php endwhile; ?>
</ul>

What I'd like to do is have the link to the article (for instance) red if it's from category 1 and green if it's from category 2. Can you help me?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Setting aside that you're incorrectly using query_posts(), the easiest solution is to add a call to post_class(), to output post-specific classes, including .category-{ID} and .category-{slug}:

<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    <li <?php post_class(); ?>><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><strong><?php the_title(); ?></strong></a><br />
    <em><?php echo substr(get_the_excerpt(), 0,110); ?> ...</em>
    </li>
    <br />
<?php endwhile; ?>

Then, you can use CSS to target li.category-{ID}.

share|improve this answer
    
Can you elaborate more on why the use of query_posts is wrong? I didn't call an endif? –  MultiformeIngegno Nov 19 '13 at 19:18
    

This isn't really the most dynamic solution but you could do something like this:

<ul>
<?php query_posts('cat=1,2&showposts=6'); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

  <?php if($post->post_category == 1) : ?>
    <li style="color:red">
  <?php else : ?>
    <li style="color:green">
  <?php endif; ?>

        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><strong><?php the_title(); ?></strong></a><br />
        <em><?php echo substr(get_the_excerpt(), 0,110); ?> ...</em>
    </li>
    <br />
<?php endwhile; ?>
</ul>

Alternatively you could dynamically add a class for each category, here I'm basing it on slug but the best solution might be to base it on term_id Codex

<ul>
<?php query_posts('cat=1,2&showposts=6'); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    <?php $cat = get_the_category(); ?>
    <li class="catColor-<?php echo $cat->slug; ?>">
        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><strong><?php the_title(); ?></strong></a><br />
        <em><?php echo substr(get_the_excerpt(), 0,110); ?> ...</em>
    </li>
    <br />
<?php endwhile; ?>
</ul>

If you are familiar enough with wordpress / php you could take a shot at adding a custom field to your categories to add colors there. Here's How I did That

share|improve this answer
    
Thanks! interesting :-) –  MultiformeIngegno Nov 19 '13 at 19:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.