WordPress.org

Forums

Background color based on category (8 posts)

  1. tab0429
    Member
    Posted 1 year ago #

    I'm trying to add a background color to a div based on the category the post belongs in using the code below

    echo '  <div class= "thumb"><a href="' . get_permalink() . '">' . get_the_post_thumbnail() . '</a></div>
          		  <div class= "catcolor">
    
    	  if ( in_category( '6' ) ) {
    
    	  echo '<div class= "thumbcat1">';
    
    	  } elseif ( in_category( '7' ) ) {
    
    	  echo '<div class= "thumbcat1">';
    
    	  } elseif ( in_category( '8' ) ) {
    
    	  echo '<div class= "thumbcat2">';
    
    	  } elseif ( in_category( '9' ) ) {
    
    	  echo '<div class= "thumbcat2">';
    
    	  } elseif ( in_category( '10' ) ) {
    
    	  echo '<div class= "thumbcat3">';
    
    	  } elseif ( in_category( '11' ) ) {
    
    	  echo '<div class= "thumbcat3">';
    
    	  } elseif ( is_category() ) {
    
    	  	        echo 'background-color: #000';
    
    	  }
    
          		  </div>
    			  <div class= "title"><h3><a href="' . get_permalink() .'">' . get_the_title() . '</a></h3> </div>';

    but it's not working... I keep getting the error

    Parse error: syntax error, unexpected T_LNUMBER, expecting ',' or ';' in /home/soulofan/public_html/wplearn/wp-content/themes/Scharc/archive2.php on line 24

    what am I doing wrong?
    I'm not a php coder and this is the first theme I am building.
    Here's the link
    I appreciate the help, thanks!

  2. alchymyth
    Forum Moderator
    Posted 1 year ago #

    the WordPress functions post_class() would output a category dependent class:
    http://codex.wordpress.org/Function_Reference/post_class

    you need to watch the quotation marks of your echo strings.

    for example:

    echo '  <div class= "thumb"><a href="' . get_permalink() . '">' . get_the_post_thumbnail() . '</a></div>
          		  <div class= "catcolor">';
    
    	  if ( in_category( '6' ) ) {
    
    	  echo '<div class= "thumbcat1">';
    
    	  } elseif ( in_category( '7' ) ) {
    
    	  echo '<div class= "thumbcat1">';
    
    	  } elseif ( in_category( '8' ) ) {
    
    	  echo '<div class= "thumbcat2">';
    
    	  } elseif ( in_category( '9' ) ) {
    
    	  echo '<div class= "thumbcat2">';
    
    	  } elseif ( in_category( '10' ) ) {
    
    	  echo '<div class= "thumbcat3">';
    
    	  } elseif ( in_category( '11' ) ) {
    
    	  echo '<div class= "thumbcat3">';
    
    	  } elseif ( is_category() ) {
    
    	  	        echo 'background-color: #000';
    
    	  }
    
          		echo '</div>
    			  <div class= "title"><h3><a href="' . get_permalink() .'">' . get_the_title() . '</a></h3> </div>';

    also, this line echo 'background-color: #000'; does not make much sense, as you are outputting a background declaration instead of a div(?)

  3. tab0429
    Member
    Posted 1 year ago #

    That was a mistake, the last echo there, it was right in the code, I must have copied wrong. Anyway, I made some changes and though I don't get an error, it is not working. I got it to work as a background color if it was all in one div, but as soon as I put the thumb and title in their own divs I started having problems.

    Here's the page now, without errors but still not working. What am I missing? Thanks so much for your help!!

  4. zex2911
    Member
    Posted 1 year ago #

    Hello
    If you inspect element, you will notice that all of the data that needs to appear in the 'head' tag appears in the 'body' tag. Check the header.php for anything that's not closed, that you call wp_head correctly...

  5. tab0429
    Member
    Posted 1 year ago #

    You're right zex2911 and that is something I have to fix, but that's not the issue I'm really struggling with!

    I took out the if else code from the page, and now it displays properly (horizontal and with the tile). What I want is a color bar between the picture and the text. The color should reflect the category of the post. What is the best way to do this? Thanks!

  6. zex2911
    Member
    Posted 1 year ago #

    Try using

    $category = get_the_category();
    $catBgColor = $category[0]->cat_name;

    inside of loop (if you only have 1 category per post)
    and then change if else code to something like

    if ($catBgColor = 'someCat') {
    $thumbCat = 1;
    }
    elseif ($catBgColor = 'someOtherCat') {
    $thumbcat = 2;
    }...
    else {
    $thumbcat=b1 //if is any other category create class of thumbcatb1 with black background
    }

    And then

    <div class=thumbcat<?php $thumbcat; ?>>
  7. zex2911
    Member
    Posted 1 year ago #

    also if you have same background for multiple categories you can use
    if ( ($catBgColor = 'someCat') || ($catBgColor = 'SameBgCat') ){
    Hope that helps

  8. tab0429
    Member
    Posted 1 year ago #

    Thanks for your help Zeljko. I can't seem to get it to work :(
    I have a proposition for you! Can you send me an email at tabata.young at gmail . com with your contact info?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.