$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>categories - Set colors depending on category|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

categories - Set colors depending on category

matteradmin10PV0评论

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?

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 asked Nov 12, 2013 at 17:45 MultiformeIngegnoMultiformeIngegno 2905 silver badges26 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

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}.

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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far