title: WordPress: How can I add extra classes via variables when using body_class()?
body: <p>I am trying to add some dynamically (based on the URI) created class names into the standard <code>body_class()</code> statement.</p>
  
  <p>The wordpress codex mentions to put the class into the brackets while using the <code>body_class('add-class-here')</code></p>
  
  <p>however I have 2 variables that I need to echo out inside the <code>body class=""</code> so I tried doing it as follows:</p>
  
  <pre><code>&lt;?php
  $url = explode('/', $_SERVER['REQUEST_URI']);
  $dir = $url[2] ? $url[2] : 'home';
  $subdir = $url[3] ? $url[3] : '';
  ?&gt;
  
  &lt;body &lt;?php body_class(&lt;?=$dir?&gt;&lt;?=($subdir?' ':'')?&gt;&lt;?=$subdir?&gt;); ?&gt;&gt;
  </code></pre>
  
  <p>This however results in a PHP error thus breaking the page.</p>
  
  <p>I tried to add <code>body_class($dir)</code> and while it works, when adding the second variable <code>$subdir</code> it fails.</p>
  
  <p>eg. <code>body_class($dir($subdir?' ':'')$subdir)</code> results in:
  <code>Parse error: syntax error, unexpected T_VARIABLE</code></p>
  
  <p>the <code>($subdir?' ':'')</code> is only there to add a space between class names if <code>$subdir</code> is set.</p>
  
  <p>Any ideas how I can add my variables into the body class while keeping the standard generated classes of the <code>body_class()</code> function?</p>
  
  <p>Thanks for reading.</p>