WordPress.org

Forums

[Complicated?] How can I show a different image for each category? (2 posts)

  1. apcarry
    Member
    Posted 1 year ago #

    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> ';

  2. Dave Naylor
    Member
    Posted 1 year ago #

    Maybe make use of a plugin such as:

    http://wordpress.org/plugins/categories-images/

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.