Support » Fixing WordPress » Linking category images to links

  • Resolved richarduk


    I’m interested in having a front page with images for each category. Let’s say the website is about Fruit. On the front page there will be a picture of an apple, a pear, an orange etc. Clicking on the picture will then take me to a category of posts on apples, pears, oranges etc.

    The way I’ve half figured it is to create the front page with one post. This post is associated with all the appropriate categories.

    Then I use this code from the Codex within The Loop. It basically says, display an image for every category that this post is in. I can put each one in a div and have no content for the post, so that the front page is taken up with a series of large images:

    <?php foreach((get_the_category()) as $cat)
     { echo '<img src="' . $cat->cat_ID . '.jpg"
    alt="' . $cat->cat_name . '" />'; } ?>

    Or similar. Using nice names, whatever. And having each image appropriately named.

    I can sort that out. But what I can’t figure out is how to put a permalink in. I don’t want to use plugins, because I’d be dependent on plugin authors keeping plugins updated whenever WP is updated.

    Has anyone got any very clever ideas?


Viewing 2 replies - 1 through 2 (of 2 total)
  • moshu




    Thanks, Moshu.

    For anyone else who’s interested, follow the link above. The following code is basically a tweak of Davidemorgan, Otto42 and Kafkaesqui’s code.

    $image_path = get_bloginfo('stylesheet_directory');
    $cats = get_categories('child_of=22');
    foreach ((array)$cats as $cat) {
    	$catdesc = attribute_escape(strip_tags($cat->category_description));
    	$image = $cat->category_nicename .'.jpg';
    		$cat_img = '<div class="category-image" id="cat-image-'.$count.'"'.'><a href="' . get_category_link($cat) . '" title="'. $catdesc .'"><img class="image-for-category" src="' . $image_path . '/images/' . $image . '" alt="' . $cat->cat_name . '" /></a></div>'."\n\n";
    	echo  $cat_img ;
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Linking category images to links’ is closed to new replies.