snipt

Ctrl+h for KB shortcuts

PHP

WordPress: menu code to display a logged-in or logged-out menu

1
2
3
4
5
6
<?php
if ( is_user_logged_in() ) {
     wp_nav_menu( array( 'theme_location' => 'logged-in-menu' ) );
} else {
     wp_nav_menu( array( 'theme_location' => 'logged-out-menu' ) );
}
https://snipt.net/embed/44bd86fba7c503699d31fc5c76181aff/
https://snipt.net/raw/44bd86fba7c503699d31fc5c76181aff/
44bd86fba7c503699d31fc5c76181aff
php
PHP
6
2016-09-23T13:58:42
True
False
False
/api/public/snipt/142383/
wordpress-menu-code-to-display-a-logged-in-or-logged-out-menu
<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></pre></div></td><td class="code"><div class="highlight"><pre><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="k">if</span> <span class="p">(</span> <span class="nx">is_user_logged_in</span><span class="p">()</span> <span class="p">)</span> <span class="p">{</span> </span><span id="L-3"><a name="L-3"></a> <span class="nx">wp_nav_menu</span><span class="p">(</span> <span class="k">array</span><span class="p">(</span> <span class="s1">&#39;theme_location&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;logged-in-menu&#39;</span> <span class="p">)</span> <span class="p">);</span> </span><span id="L-4"><a name="L-4"></a><span class="p">}</span> <span class="k">else</span> <span class="p">{</span> </span><span id="L-5"><a name="L-5"></a> <span class="nx">wp_nav_menu</span><span class="p">(</span> <span class="k">array</span><span class="p">(</span> <span class="s1">&#39;theme_location&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;logged-out-menu&#39;</span> <span class="p">)</span> <span class="p">);</span> </span><span id="L-6"><a name="L-6"></a><span class="p">}</span> </span></pre></div> </td></tr></table>
menu, template-tag, wordpress

PHP

Add first/last classes to menu in WordPress

1
2
3
4
5
6
7
8
<?php

add_filter('wp_nav_menu', 'cam_menu');
function cam_menu($output) {
	$output = preg_replace('/class="menu-item/', 'class="menu-item-first menu-item', $output, 1);
	$output = substr_replace($output, 'class="menu-item-last menu-item', strripos($output, 'class="menu-item'), strlen('class="menu-item'));
	return $output;
}
https://snipt.net/embed/912b67b5dd877828e65e8fa4cc76d167/
https://snipt.net/raw/912b67b5dd877828e65e8fa4cc76d167/
912b67b5dd877828e65e8fa4cc76d167
php
PHP
8
2016-09-21T14:49:27
True
False
False
Jul 14, 2013 at 02:55 AM
/api/public/snipt/66896/
add-firstlast-classes-to-menu-in-wordpress
<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 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><span id="L-3"><a name="L-3"></a><span class="nx">add_filter</span><span class="p">(</span><span class="s1">&#39;wp_nav_menu&#39;</span><span class="p">,</span> <span class="s1">&#39;cam_menu&#39;</span><span class="p">);</span> </span><span id="L-4"><a name="L-4"></a><span class="k">function</span> <span class="nf">cam_menu</span><span class="p">(</span><span class="nv">$output</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-5"><a name="L-5"></a> <span class="nv">$output</span> <span class="o">=</span> <span class="nb">preg_replace</span><span class="p">(</span><span class="s1">&#39;/class=&quot;menu-item/&#39;</span><span class="p">,</span> <span class="s1">&#39;class=&quot;menu-item-first menu-item&#39;</span><span class="p">,</span> <span class="nv">$output</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span> </span><span id="L-6"><a name="L-6"></a> <span class="nv">$output</span> <span class="o">=</span> <span class="nb">substr_replace</span><span class="p">(</span><span class="nv">$output</span><span class="p">,</span> <span class="s1">&#39;class=&quot;menu-item-last menu-item&#39;</span><span class="p">,</span> <span class="nb">strripos</span><span class="p">(</span><span class="nv">$output</span><span class="p">,</span> <span class="s1">&#39;class=&quot;menu-item&#39;</span><span class="p">),</span> <span class="nb">strlen</span><span class="p">(</span><span class="s1">&#39;class=&quot;menu-item&#39;</span><span class="p">));</span> </span><span id="L-7"><a name="L-7"></a> <span class="k">return</span> <span class="nv">$output</span><span class="p">;</span> </span><span id="L-8"><a name="L-8"></a><span class="p">}</span> </span></pre></div> </td></tr></table>
menu, wordpress
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.