WordPress.org

Ready to get started?Download WordPress

Forums

How to give posts on mainpage different layout depending on category? (3 posts)

  1. maartenbouten
    Member
    Posted 7 years ago #

    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">
    <br>
    
               <?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;'); ?>
    				</div>
    
    				<p class="postmetadata"><!--Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> --><?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?>
    
    			</div>
    
    <address></address>
    		<?php endwhile; ?>
  2. Marcel Brinkkemper
    Member
    Posted 7 years ago #

    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(); ?>">
    to
    <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.

  3. nolageek
    Member
    Posted 6 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic