Support » Fixing WordPress » Category Pictures

  • Resolved Karin24

    (@karin24)


    Hi again,

    another important question:
    I would like to have a small picture on every category site which explains the category. For example: For the category “fire” there should be a picture of a burning tree and for the categorie “water” there should be a picture of a raindrop.

    I wanted to solve this problem by naming the pictures by its ID. So the picture for the category fire (CAT=6) is called xxxx6.jpg. You can see it in the following code:

    <table border="0">
      <?php foreach((get_the_category()) as $cat) { echo '<tr><td><a href="http://[mydomain]/database/?cat=' . $cat->cat_ID . '"><img src="http://mydomain]/images/projects-icon-big-' . $cat->cat_ID . '.gif" border="0" hspace="2" vspace="2" alt="' . $cat->cat_name . '" /></td><td>In the following you will find all our projects within the category ' . $cat->cat_name . '.</a></td></tr>'; } ?>
    </table>

    This works absolutely fine BUT not just the picture of the category is shown by my browser. My browser shows ALL PICTURES OF THE FIRST POST OF THIS CATEGORY. If the first post within this category is connected with two categories, both categorie pictures are shown by my browser.

    Because i think that this is a problem with my code i will post it completely in the following:

    <?php is_tag(); ?>
    
    <?php if (have_posts()) : ?>
    
     	  <?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>
    
    <?php /* If this is a category archive */ if (is_category()) { ?>
    Archive for the category <?php single_cat_title(); ?>
    
    <?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>
    Archive for the tag <?php single_tag_title(); ?>
    
    <?php } ?>
    
    <table border="0">
      <?php foreach((get_the_category()) as $cat) { echo '<tr><td><a href="http://[mydomain]/projects/database/?cat=' . $cat->cat_ID . '"><img src="http://[mydomain]/images/projects-icon-big-' . $cat->cat_ID . '.gif" border="0" hspace="2" vspace="2" alt="' . $cat->cat_name . '" /></td><td>In the following you will find all stuff within the category ' . $cat->cat_name . '.</a></td></tr>'; } ?>
    </table>
    
    <?php while (have_posts()) : the_post(); ?>
    
        <h3 id="post-<?php the_ID(); ?>">
          <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
        </h3>
    
    <?php endwhile; ?>

    Please help me!!! Thanks, Karin

Viewing 3 replies - 1 through 3 (of 3 total)
  • Try this code

    <?php /* If this is a category archive */ if (is_category()) { ?>
    Archive for the category <?php single_cat_title(); ?>
    <?php $cat = intval( get_query_var('cat') );
    	if ( !empty($cat) && !(strtoupper($cat) == 'ALL') ) {?>
    <img src="http://mydomain]/images/projects-icon-big-' . <?php echo $cat; ?> . '.gif" border="0" hspace="2" vspace="2" alt="' . <?php single_cat_title(); ?>. '" />
    <?php } ?>

    Code is working fine, thanks! You are a genius! 🙂

    Or your can use the Category Icons plugin :
    http://www.category-icons.com

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Category Pictures’ is closed to new replies.