WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Linking category images to links (3 posts)

  1. richarduk
    Member
    Posted 6 years ago #

    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="http://example.com/images/' . $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?

    Thanks!

  2. moshu
    Member
    Posted 6 years ago #

  3. richarduk
    Member
    Posted 6 years ago #

    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.

    <?php
    $image_path = get_bloginfo('stylesheet_directory');
    $cats = get_categories('child_of=22');
    $count=1;
    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 ;
    	$count++;
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic