Support » Fixing WordPress » How to give posts on mainpage different layout depending on category?

  • Hello all,

    could anybody help me with this question? “How to give posts on the mainpage a different layout depending on category they are in?” For example: display the posts which are in a specific category with specific background color, title color or icon (image) included in the post.

    I have tried some things, but those damaged the page 🙁 .

    Part of the code of my index file is below.

    Thanks very much in advance.

    <?php get_header(); ?>
    	<div id="content" class="narrowcolumn">
               <?php if (have_posts()) : ?> 
    		<?php while (have_posts()) : the_post(); ?>
    			<div class="post" id="post-<?php the_ID(); ?>">
    <!-- This is the image I would like to change depending on the category the post is in. -->
    <img src="images/a.jpg" width="30" height="30" alt="hs" align="left" class="imgcat3"/>
                                    <a href="<?php the_permalink() ?>" class="title" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a><br>
    				<small><?php the_time('F jS, Y') ?>, <!-- by <?php the_author() ?> -->    filed under <?php the_category(', ') ?></small>
    				<div class="entry">
    					<?php the_content('Read the rest of this entry &raquo;'); ?>
    				<p class="postmetadata"><!--Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> --><?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?>
    		<?php endwhile; ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • maarten,
    you could make jpg files with the same name as your category like mycategory.jpg
    and use <?php the_category() ?>.jpg as the filename in the script
    or make classes named after your categories and do the formatting in your style.css.
    then change <div class="post" id="post-<?php the_ID(); ?>">
    <div class="<?php the_category() ?>" id="post-<?php the_ID(); ?>">
    This will only work if you only have one category per post.
    Probably there is also a plugin for this somewhere.

    How do we stop <?php the_category() ?> from linking the category name in 2.32?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to give posts on mainpage different layout depending on category?’ is closed to new replies.