<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Digging into WordPress &#187; body</title>
	<atom:link href="http://digwp.com/tag/body/feed/" rel="self" type="application/rss+xml" />
	<link>http://digwp.com</link>
	<description>Take your WordPress skills to the next level.</description>
	<lastBuildDate>Thu, 09 Feb 2012 19:03:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Advanced WordPress Targeting with body_class_plus()</title>
		<link>http://digwp.com/2009/08/wordpress-body-class-plus/</link>
		<comments>http://digwp.com/2009/08/wordpress-body-class-plus/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 03:56:30 +0000</pubDate>
		<dc:creator>Jeff Starr</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Plugins]]></category>
		<category><![CDATA[body]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[tags]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=493</guid>
		<description><![CDATA[Since WordPress 2.8, we can target specific types of page views with CSS using the new body_class() tag. Designed for use within the &#60;body&#62; element, body_class() outputs various class attributes according to the current type of page view. This makes it easy to apply page-specific styling such as current-page navigation highlighting and other nifty CSS [...]]]></description>
			<content:encoded><![CDATA[<p>Since WordPress 2.8, we can target specific types of page views with <acronym title="Cascading Style Sheets">CSS</acronym> using the new <code>body_class()</code> tag. Designed for use within the <code>&lt;body&gt;</code> element, <code>body_class()</code> outputs various class attributes according to the current type of page view. This makes it easy to apply page-specific styling such as current-page navigation highlighting and other nifty <acronym title="Cascading Style Sheets">CSS</acronym> tricks.</p>
<p><span id="more-493"></span></p>
<h3>Strengths and shortcomings of the <code>body_class()</code> tag</h3>
<p>For the uninitiated, here is a quick review of the <code>body_class()</code> tag. When used as follows:</p>
<pre><code>&lt;body &lt;?php body_class(); ?&gt;&gt;</code></pre>
<p>The <code>body_class()</code> tag will output the following class names depending on current page type:</p>
<ul>
<li><code>.home</code> &#8212; class name output for the home page</li>
<li><code>.archive</code> &#8212; class name output for any archive page</li>
<li><code>.category</code> &#8212; class name output for any category page</li>
<li><code>.search</code> &#8212; class name output for any search page</li>
<li><code>.tag</code> &#8212; class name output for any tag archive page</li>
<li><a href="#attributes" title="View a complete list of body_class() attribute values">Plus many more..</a></li>
</ul>
<p>So, for example, when you are logged in to WordPress and viewing your blog&rsquo;s home page, the <code>body_class()</code> tag will result in something like this:</p>
<p><code>&lt;body class="home blog logged-in"&gt;</code></p>
<p>Or, if you were logged out and visiting your &ldquo;Asides&rdquo; category archive, your source code would include this:</p>
<p><code>&lt;body class="archive category category-asides"&gt;</code></p>
<p>This functionality enables you to, say, apply styles to the <code>&lt;h1&gt;</code> heading elements on all of your search pages:</p>
<pre><code>body.search h1 {
	border-bottom: 1px solid #ffff99;
	color: #ffff99;
	}</code></pre>
<p>Likewise, the class names output by the <code>body_class()</code> tag make it possible to target conditionally <em>any</em> element on <em>any</em> type of page, thereby greatly increasing the stylistic flexibility of WordPress. Yet despite its awesomeness, <code>body_class()</code> is essentially an &ldquo;all-or-nothing&rdquo; proposition. We can apply styles to <em>all</em> category pages, <em>all</em> search pages, <em>all</em> page pages, and so on. We can even use <acronym title="Cascading Style Sheets">CSS</acronym> chaining to target all category views when the user is logged in:</p>
<p><code>.home.logged-in {
	border: 100px solid red;
	}</code></p>
<p>This is powerful, yes, but what if we want to target, say, all categories that include the term &ldquo;services&rdquo; in the category slug? Sure, we could declare each of them individually, like this:</p>
<pre><code>.category-coffee-service { color: red; }
.category-brunch-services { color: red; }
.category-dinner-services { color: red; }
.category-snacks-services { color: red; }
.category-drinks-services { color: red; }
.category-inside-services { color: red; }
.category-design-services { color: red; }
.category-health-services { color: red; }
.category-services-info { color: red; }
.category-services-data { color: red; }
.category-services-cost { color: red; }</code></pre>
<p>But that&rsquo;s pretty inefficient. Plus, what if the user/client needed to create new &ldquo;services-related&rdquo; categories? Have fun explaining to them how edit the <acronym title="Cascading Style Sheets">CSS</acronym> file with the appropriate rules.</p>
<p>We could, I suppose, use <acronym title="Cascading Style Sheets">CSS</acronym>-3&rsquo;s wildcard substring-matching attribute-selector:</p>
<p><code>body[class*="services"] { color: red; }</code></p>
<p>Definitely cleaner, but there a significant number of browsers that do not support <acronym title="Cascading Style Sheets">CSS</acronym> 3, so this is less than an ideal solution. </p>
<p>A better solution would be to add pattern-matching functionality to the <code>body_class()</code> tag. After all, <code>body_class()</code> keeps track of just about every possible type of page-view available on your site. So why not filter the output of <code>body_class()</code> for any instances of &ldquo;services&rdquo; and output a specific class-attribute for all matching pages. This would allow us to target and apply styles to virtually <em>any</em> subset of page-views, including those new &ldquo;services-related&rdquo; categories for our client site. Say &ldquo;goodbye&rdquo; to manual code updating and say &ldquo;hello&rdquo; to automated advanced targeting with <code>body_class_plus()</code>.</p>
<h3>Advanced targeting with <code>body_class_plus()</code></h3>
<p>The <code>body_class_plus()</code> tag extends the functionality of <code>body_class()</code> by using <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>&rsquo;s output buffering to conditionally evaluate its contents. This functionality enables us to pattern-match for specific phrases, and execute conditional operations based on the contents of <code>body_class()</code>.</p>
<p>Here is the <code>body_class_plus()</code> function that may be placed in your <code>functions.php</code> file:</p>
<p><strong>Update:</strong> There is a new &amp; improved version of this code <a href="#downloads" title="Download the latest version of this code">available via plugin</a>!</p>
<pre><code>&lt;?php // http://digwp.com/2009/08/wordpress-body-class-plus/
function body_class_plus() { 
	if(function_exists('body_class')) { 
		ob_start(); body_class(); 
		$class = ob_get_contents(); 
		ob_end_clean(); 
		if($class) {
			if(preg_match("/services/i", $class)) { 
				echo 'class="services"';
			} elseif(preg_match("/products/i", $class)) {
				echo 'class="products"';
			} elseif(preg_match("/business/i", $class)) {
				echo 'class="business"';
			} elseif(preg_match("/pleasure/i", $class)) {
				echo 'class="pleasure"';
			} else {
				echo 'class="no-body-class-matches"';
			}
		} else { 
			echo 'class="body-class-not-available"'; 
		} 
	}
} ?&gt;</code></pre>
<p>To call this function in your theme template, include the following tag in the element of your choice (e.g., within the <code>&lt;body&gt;</code> element):</p>
<pre><code>&lt;?php if(function_exists('body_class_plus')) { body_class_plus(); } ?&gt;</code></pre>
<p>This is a exemplified version of the <code>body_class_plus()</code> function that enables us to target and apply specific class attributes to any page or category containing the following terms in the <acronym title="Uniform Resource Locator">URL</acronym>/page-slug:</p>
<ul>
<li>&ldquo;<code>services</code>&rdquo; &#8212; will apply <code>class="services"</code></li>
<li>&ldquo;<code>products</code>&rdquo; &#8212; will apply <code>class="products"</code></li>
<li>&ldquo;<code>business</code>&rdquo; &#8212; will apply <code>class="business"</code></li>
<li>&ldquo;<code>pleasure</code>&rdquo; &#8212; will apply <code>class="pleasure"</code></li>
</ul>
<p>This makes it easy to match specific groups of categories, pages, posts, or any combination thereof. Even better, the <code>body_class_plus()</code> function makes it possible to do more than apply classes to targeted page types. In addition to, or instead of, applying classes to specific groups of pages, you can implement any type of functionality desired. For example, you could display template tags, echo content, or execute custom scripts depending on page type. The possibilities are endless.</p>
<h3 id="downloads">Download the plugin</h3>
<p>If custom functions aren&rsquo;t your thang, and you would rather implement this functionality via plugin, you can grab it here: </p>
<ul>
<li><a href="http://digwp.com/plugins/body-class-plus_v02.zip" title="Version 0.2 (latest version)">body-class-plus_v02.zip</a> (latest version)</li>
<li><a href="http://digwp.com/plugins/body-class-plus_v01.zip" title="Version 0.1 (original release)">body-class-plus_v01.zip</a> (original release)</li>
</ul>
<p>Thanks to <a href="http://kahi.cz/wordpress/" title="Kahi's WordPress Notes">Peter Kahoun</a> for contributing his expertise and improving the functionality of this plugin.</p>
<h3>Take-home message</h3>
<p>The take-home message is that the <code>body_class_plus()</code> function extends the functionality of the <code>body_class()</code> tag to target and apply styles to any particular subset or combination of page types. Further, the <code>body_class_plus()</code> function takes <code>body_class()</code> beyond the realm of <acronym title="Cascading Style Sheets">CSS</acronym>, making it possible to apply template tags, <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>, and other scripts to virtually any custom-defined set of pages and page types.</p>
<h3 id="attributes">Complete list of body_class() attribute values</h3>
<p>Here is a complete list of the available classes for the <code>body_class()</code> tag:</p>
<ul>
<li>rtl</li>
<li>home</li>
<li>blog</li>
<li>archive</li>
<li>date</li>
<li>search</li>
<li>paged</li>
<li>attachment</li>
<li>error404</li>
<li>single postid-(id)</li>
<li>attachmentid-(id)</li>
<li>attachment-(mime-type)</li>
<li>author</li>
<li>author-(user_nicename)</li>
<li>category</li>
<li>category-(slug)</li>
<li>tag</li>
<li>tag-(slug)</li>
<li>page-parent</li>
<li>page-child parent-pageid-(id)</li>
<li>page-template page-template-(template file name)</li>
<li>search-results</li>
<li>search-no-results</li>
<li>logged-in</li>
<li>paged-(page number)</li>
<li>single-paged-(page number)</li>
<li>page-paged-(page number)</li>
<li>category-paged-(page number)</li>
<li>tag-paged-(page number)</li>
<li>date-paged-(page number)</li>
<li>author-paged-(page number)</li>
<li>search-paged-(page number)</li>
</ul>
<p>For more ways to dynamically set body IDs, check out my article, <a href="http://perishablepress.com/press/2009/05/26/dynamic-body-class-id-php-wordpress/" title="9 Ways to Set Dynamic Body IDs via PHP and WordPress">9 Ways to Set Dynamic Body IDs via PHP and WordPress</a>.</p>
<hr />
<p><small>© 2009 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2009/08/wordpress-body-class-plus/">Permalink</a> | <a href="http://digwp.com/2009/08/wordpress-body-class-plus/#comments">16 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2009/08/wordpress-body-class-plus/&title=Advanced WordPress Targeting with body_class_plus()">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/body/" rel="tag">body</a>, <a href="http://digwp.com/tag/css/" rel="tag">CSS</a>, <a href="http://digwp.com/tag/functions/" rel="tag">functions</a>, <a href="http://digwp.com/tag/php/" rel="tag">PHP</a>, <a href="http://digwp.com/tag/plugin/" rel="tag">plugin</a>, <a href="http://digwp.com/tag/tags/" rel="tag">tags</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2009/08/wordpress-body-class-plus/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Unique Body ID&#8217;s for your Pages</title>
		<link>http://digwp.com/2009/05/unique-body-ids-for-your-pages/</link>
		<comments>http://digwp.com/2009/05/unique-body-ids-for-your-pages/#comments</comments>
		<pubDate>Sat, 30 May 2009 16:46:51 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[body]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[header]]></category>
		<category><![CDATA[permalink]]></category>

		<guid isPermaLink="false">http://modernwordpress.com/?p=18</guid>
		<description><![CDATA[There are many reasons you might want to get a unique ID for your tag. Let&#8217;s say you want your header elements to be a different color on your About page, you could style: h3 { color: blue; } body#about h3 { color: red; } But how do you get that ID on the About [...]]]></description>
			<content:encoded><![CDATA[<p>There are many reasons you might want to get a unique ID for your <tt><body></tt> tag. Let&#8217;s say you want your header elements to be a different color on your About page, you could style:</p>
<pre><code>h3 { color: blue; }
body#about h3 { color: red; }</code></pre>
<p>But how do you get that ID on the About page, but not on any other page? If you have your permalinks set up to show the page name, like <code>http://yoursite.com/about/</code>, you can use PHP to extract the &#8220;about&#8221; part of that URL.</p>
<p>Here is the magic:</p>
<pre><code>&lt;?php
	$url = explode('/',$_SERVER['REQUEST_URI']);
	$dir = $url[1] ? $url[1] : 'home';
?&gt;

&lt;body id="&lt;?php echo $dir ?&gt;"&gt;</code></pre>
<hr />
<p><small>© 2009 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2009/05/unique-body-ids-for-your-pages/">Permalink</a> | <a href="http://digwp.com/2009/05/unique-body-ids-for-your-pages/#comments">5 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2009/05/unique-body-ids-for-your-pages/&title=Unique Body ID&#8217;s for your Pages">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/body/" rel="tag">body</a>, <a href="http://digwp.com/tag/css/" rel="tag">CSS</a>, <a href="http://digwp.com/tag/header/" rel="tag">header</a>, <a href="http://digwp.com/tag/permalink/" rel="tag">permalink</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2009/05/unique-body-ids-for-your-pages/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

