Blog

Create a Class for Body Tag Using PHP File Name
Posted on April 22, 2013 in PHP by Matt Jennings

/***** Code Inside PHP Include *****/
<?php
//Create a Class for Body Tag Using PHP File Name
function body_class()
{
	$body_call_var = $_SERVER['REQUEST_URI'];

	if($body_call_var == '/index.php' || $body_call_var == '/')
	{
		$body_call_var = 'home';
	}
	else
	{
		$body_call_var = str_replace('/', '', $body_call_var);
		$body_call_var = str_replace('.php', '', $body_call_var);
	}

	echo 'class="'. $body_call_var .'"';
}
?>

/***** Function Call Inside Opening Body Tag *****/
<body <?php body_class(); ?>>

Leave a Reply