snipt

Ctrl+h for KB shortcuts

PHP

Add custom body classes to custom Wordpress templates

1
2
3
4
5
6
7
8
<?php
	//Add custom body classes to the chosen templates, for example, custom page templates that are used actually as post archives. 
	//Usage: add_filter('body_class','browser_body_class'); in the template file code
	function browser_body_class($classes = '') {
		$classes[] = 'page-customtemplate-archive';
		return $classes;
	}
?>
https://snipt.net/embed/125eaba16eacbfcae00cf66c3bb1e53e/
/raw/125eaba16eacbfcae00cf66c3bb1e53e/
125eaba16eacbfcae00cf66c3bb1e53e
php
PHP
8
2016-12-26T02:24:40
True
False
True
Jul 27, 2013 at 10:37 AM
/api/public/snipt/69875/
add-custom-body-classes-to-custom-wordpress-templates
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a> <a href="#L-8">8</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> </span><span id="L-2"><a name="L-2"></a> <span class="c1">//Add custom body classes to the chosen templates, for example, custom page templates that are used actually as post archives. </span> </span><span id="L-3"><a name="L-3"></a> <span class="c1">//Usage: add_filter(&#39;body_class&#39;,&#39;browser_body_class&#39;); in the template file code</span> </span><span id="L-4"><a name="L-4"></a> <span class="k">function</span> <span class="nf">browser_body_class</span><span class="p">(</span><span class="nv">$classes</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-5"><a name="L-5"></a> <span class="nv">$classes</span><span class="p">[]</span> <span class="o">=</span> <span class="s1">&#39;page-customtemplate-archive&#39;</span><span class="p">;</span> </span><span id="L-6"><a name="L-6"></a> <span class="k">return</span> <span class="nv">$classes</span><span class="p">;</span> </span><span id="L-7"><a name="L-7"></a> <span class="p">}</span> </span><span id="L-8"><a name="L-8"></a><span class="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
class, css, functions.php, template, wordpress