WordPress.org

Support

Support » How-To and Troubleshooting » different backgroundimages for each category on the mainpage

different backgroundimages for each category on the mainpage

  • Echnaton69
    Member

    @echnaton69

    hi,

    I’ve searched around but didn’t find an answer.

    Is it possible (and if yes how? 😉 ) to show a different background-image (oder color) for an article on the main page where the image (or color) depends on the category the article is written in?

    for example a post on the mainpage from the category “fish” shows a blue background and a post from the category tree a green one.

    Thank you and

    kind regards from Vienna

    Andreas

Viewing 10 replies - 1 through 10 (of 10 total)
  • Orin
    Member

    @orin

    If you wanted to have <img> tags put in for each category, it would be pretty simple to use the Category Images plugin. However, what I think you’re seeking sounds like it’d be easier accomplished using php to define a unique class for each category, then assigning unique styles.

    <?php if (is_category('about')) { ?>
    class="cat-about"
    <?php } elseif (is_category('books')) { ?>
    class="cat-books"
    <?php } elseif ...
    (etc) { ?>
    <?php } ?>

    Learn more about conditional tags in the codex.

    skippy
    Member

    @skippy

    See also the Structured Blogging and Force Category Template plugins.

    I did what Orin said.
    But it doesn’t chanche anything in my site.

    Can u have a look please?
    http://www.sancarlobasket.com/wp3/

    In order to restyle your categories, you will need to write the css style for the code you want to add, or force in the conditional tag.

    Without the css in your style.css then changing the ID or CLASS won’t make any changes.

    I would look into the Force Category Template solution and unless you’re ready to change your styles around manually use various themes you like for the look of your categories.

    well the css is here

    .cat-milan {
    background-color: #000000;
    border: 1px solid red;
    }

    up

    Look at replacing the is_category() conditional Orin mentioned with in_category():

    http://codex.wordpress.org/Template_Tags/in_category

    yes, Orin and KAfkaesqui and right. I didn’t read the initial entry clearly.

    That will totally work.

    Add the conditionals for the style into your header. and add the style for each cat within the conditional.

    <?php if (is_cat (‘1’)) { ?>
    css style for cat 1

    <?php else_if (is_cat(‘2’)) { ?>
    css style for cat 2

    <?php else_if (is_cat(‘3’)) { ?>
    css style for cat 3

    <?php } ?>

    I don’t know if it’s still true, but it used to be best to write conditionals for the category or post number, and not the name to avoid problems.

    Try this, and then add it to your header and I’ll be it will work just fine.

    Actually, you want:

    <?php if (in_category('1')) { ?>
    css style for cat 1

    <?php else if (in_category('2')) { ?>
    css style for cat 2

    <?php else if (in_category('3')) { ?>
    css style for cat 3

    <?php } ?>

    I’m not sure what that is_cat() thing is… ;)

    thank u guys it helped

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘different backgroundimages for each category on the mainpage’ is closed to new replies.