Changing Font Color with PHP According to Page

Posted on 2007-12-04
Last Modified: 2012-06-27
Hello Experts,

I am new to PHP and I am using it to code a website using Wordpress as the CMS.  At the moment, each page has a different title which is displayed just beneath the menu.  I would like to use a different color (colors that I've already chosen) for the text in the title, according to which page it is.  

Meaning, the "pets" page would display the title "Pets" in green lettering...the dogs page would display the title "Dogs" in brown lettering...and so on.  

I am currently working with a combination of css and php...  I've placed the title's php code in a div tag, which is ID'd in CSS to control its font size, etc.  

Is there anyone that knows how one can change the font color for the title, according to which page is being displayed, using PHP?  

Thank you in advance...

Open in new window

Question by:TrickyFingers
LVL 16

Accepted Solution

BongSoo earned 168 total points
ID: 20403454
You could do it strictly using CSS by putting the individual titles in div tags with unique IDs. that would be easiest.

Expert Comment

ID: 20403467
If I understand what your asking,.

All you need to do is have a different class for each title, then in each page use the appropriate class name.

or if you want every page to be the same you could do the following in your php, just make sure you set the $title variable first.
if ($title == "pets") {$color = "#11ee00";}

else if ($title == "dogs") {$color = "#994400";}

else {$color = "#000000";}

echo"<h1 style=\"color: \$color;\">$title</h1>";

Open in new window


Expert Comment

ID: 20403477
You could use the switch statement, like below.

case 'pets':



case 'other':






echo "<div class='some_class' id='$id'>title</div>";

Open in new window

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.


Assisted Solution

Mr_Splash earned 166 total points
ID: 20403489
Code correction
if ($title == "pets") {$color = "#11ee00";}

else if ($title == "dogs") {$color = "#994400";}

else {$color = "#000000";}


echo"<h1 style=\"color: $color;\">$title</h1>";

Open in new window

LVL 19

Assisted Solution

darron_chapman earned 166 total points
ID: 20403621
I'm assuming you have admin rights to this wordpress installation, if you do, there is a plugin that does exactly that and it wouldn't require you to edit css or php.  ...

I tested it out and it works as expected... when you click Select Colour, the color chooser is off to the right and you need to scroll over to see it.

Expert Comment

ID: 20403828
Please see the below code and let me know if it work orif u have any quires

	//populate all of the available title in an array

	$allowedTitle = array("pets","dogs","birds");

	//get the current page name

	$currentPage = "pets";

	$cssClass = "defaultStyle";

	//here you have to write seperate CSS for each title

	// like petsStyle,dogsStyle,birdsStyle etc



		//if the current page is exists in the allowed title change the CSS 

		$cssClass = $currentPage."Style";



	<div calss="<?php echo $cssClass; ?>">Your Title</div>

Open in new window


Expert Comment

ID: 20729530
Forced accept.

EE Admin

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Mysql sync between 3-4 mysql db 4 24
Access Object's Property 9 22
datetime in sql 6 25
How do I randomize photo images before i move them to the path file? 9 20
Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
The viewer will learn how to count occurrences of each item in an array.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now