WordPress.org

Support

Support » How-To and Troubleshooting » [Complicated?] How can I show a different image for each category?

[Complicated?] How can I show a different image for each category?

  • Hey Guys,

    I have implemented code into my index page so that it shows the most recent post from each category.
    However, how can I change Category: <Category Name> to be a category specific image?

    The code for the main index block is:

    <?php
    $selected_categories=array(
     'include' => '3,4,5',
     'order' => 'ASC'
     );
    
    $categories=get_categories($selected_categories);
     foreach($categories as $category) {
     $post_args=array(
     'showposts' => 1, // you can fetch number of articles from each category
     'category__in' => array($category->term_id),
     'caller_get_posts'=>1
     );
     $posts=get_posts($post_args);
     if ($posts) {
     echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
     foreach($posts as $post) {
     setup_postdata($post); ?>
     <div>
     <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
     <?php the_title(); ?></a></p>
     <p><?php the_excerpt(); ?>
     </div>
     <?php
     } // foreach($posts
     } // if ($posts
     } // foreach($categories
    ?>

    With the line specific code being:
    echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘[Complicated?] How can I show a different image for each category?’ is closed to new replies.
Skip to toolbar