<?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>WP First Aid &#187; wp_page_menu</title>
	<atom:link href="http://wpfirstaid.com/tag/wp_page_menu/feed/" rel="self" type="application/rss+xml" />
	<link>http://wpfirstaid.com</link>
	<description>It&#039;s WordPress ... anything is possible!</description>
	<lastBuildDate>Tue, 17 Jan 2012 16:47:01 +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>Extend the WordPress Menu</title>
		<link>http://wpfirstaid.com/2010/10/extend-the-wordpress-menu/</link>
		<comments>http://wpfirstaid.com/2010/10/extend-the-wordpress-menu/#comments</comments>
		<pubDate>Thu, 07 Oct 2010 16:29:08 +0000</pubDate>
		<dc:creator>The Doctor</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[add_filter]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[wp_list_categories]]></category>
		<category><![CDATA[wp_list_pages]]></category>
		<category><![CDATA[wp_nav_menu]]></category>
		<category><![CDATA[wp_nav_menu_items]]></category>
		<category><![CDATA[wp_page_menu]]></category>

		<guid isPermaLink="false">http://wpfirstaid.com/?p=823</guid>
		<description><![CDATA[Expand the wp_nav_menu() function by adding menu items to the output with the add_filter() function.]]></description>
			<content:encoded><![CDATA[<p>I have written <a href="http://wpfirstaid.com/tag/wp_nav_menu/">several posts</a> regarding the use of the <em>wp_nav_menu()</em> function. I have written about its uses, its implementation, and even an <a href="http://wp.me/pIDVu-6K">older post</a> regarding (some of) the <abbr title="Cascading Style Sheet">CSS</abbr> classes it generates. This post continues with an idea for extending the <em>wp_nav_menu()</em> function by adding additional items to the menu list with code versus the user interface found in the Administration Panels, such as automagically adding a self-propagating list of categories.</p>
<p>This idea is not necessarily new but finding an example may be a bit more difficult. Let&#8217;s have a look &#8230;</p>
<p>First off, there are several sources of information regarding <em>wp_nav_menu()</em> to take into consideration:</p>
<ul>
<h5><em>Each of these links will open in a new tab/window for convenience purposes.</em></h5>
<li>The <a target="_blank" href="http://codex.wordpress.org/Function_Reference/wp_nav_menu">WordPress codex</a> reference.</li>
<li>The &#8220;<a target="_blank" href="http://core.trac.wordpress.org/browser/branches/3.0/wp-includes/nav-menu-template.php#L111">core.trac.wordpress.org</a>&#8221; reference.</li>
<li>This web site&#8217;s <a target="_blank" href="http://wpfirstaid.com/tag/wp_nav_menu/">references</a>.</li>
</ul>
<p>Now that we have a list of reading material we should actually take a look at it &#8230;</p>
<ul>
<li>The first reference leads to the WordPress codex and provides the standard parameters that can be used with <em>wp_nav_menu()</em>.</li>
<li>The next reference and the experience that was offered by <a href="http://iandanielstewart.com/">Ian Stewart</a> from <a href="http://themeshaper.com/">ThemeShaper.com</a> is the real key to solving this little puzzle.</li>
<li>The last reference simply leads to the <em>wp_nav_menu()</em> tag archive here.</li>
</ul>
<p>I would suggest at least having a quick look at those pages before carrying on to the next page, especially the one referencing the <em>wp_nav_menu()</em> function in the &#8220;core&#8221;, or primary code base of WordPress, as you will find a very relevant comment there.</p>
<p>Let&#8217;s start by focusing on the following code snippet as found in the &#8220;<a href="http://core.trac.wordpress.org/browser/branches/3.0/wp-includes/nav-menu-template.php#L221">core</a>&#8221; reference:</p>
<pre class="brush: php; first-line: 221; title: ; notranslate">
// Allow plugins to hook into the menu to add their own &lt;li&gt;'s
$items = apply_filters( 'wp_nav_menu_items', $items, $args );
$items = apply_filters( &quot;wp_nav_menu_{$menu-&gt;slug}_items&quot;, $items, $args );
$nav_menu .= $items;
</pre>
<p>The beauty of this little snippet is it shows, with the use of the <em>wp_nav_menu_item</em> filter, you can add specific items to the menu output if the end-user has chosen to create a custom menu. Let&#8217;s look at an example that will allow you to add a &#8220;Home&#8221; page link to the beginning of the menu.</p>
<p>If you add the following to your &#8216;functions.php&#8217; template file it will add a home link to your <em>wp_nav_menu()</em> output:</p>
<pre class="brush: php; title: ; notranslate">
// Filter wp_nav_menu() to add additional links and other output
function new_nav_menu_items($items) {
	$homelink = '&lt;li class=&quot;home&quot;&gt;&lt;a href=&quot;' . home_url( '/' ) . '&quot;&gt;' . __('Home') . '&lt;/a&gt;&lt;/li&gt;';
	$items = $homelink . $items;
	return $items;
}
add_filter( 'wp_nav_menu_items', 'new_nav_menu_items' );
</pre>
<p>The basic premise of the function is:</p>
<ol>
<li>Assign your desired output to a variable</li>
<li>Concatenate the output variable with the <em>$items</em> variable</li>
<li>Return the $items variable so it can be used in the filter</li>
<li>Add the results of the function to <em>wp_nav_menu_items</em> with an <em><a href="http://codex.wordpress.org/Function_Reference/add_filter">add_filter()</a></em> function</li>
</ol>
<div class="nota-bene">NB: It is also important to realize that this function will work as is when a custom menu is being used, but you can easily adjust the above code to take into consideration common <em>fallback</em> functions such as <em><a href="http://codex.wordpress.org/Function_Reference/wp_list_pages">wp_list_pages()</a></em> and <em><a href="http://codex.wordpress.org/Function_Reference/wp_page_menu">wp_page_menu()</a></em>.</div>
<p>If you wish to use this when a custom menu is not being used just add the following (or similar) filter after the above code:</p>
<pre class="brush: php; title: ; notranslate">
add_filter( 'wp_list_pages', 'new_nav_menu_items' );
</pre>
<p>But what if we wanted to add something to the end of the menu output, such as a <abbr title="Really Simple Syndication">RSS</abbr> feed link, or maybe just some random text or a static URL link. Let&#8217;s look at the following example which expands upon the code above to do just that.</p>
<pre class="brush: php; title: ; notranslate">
// Filter wp_nav_menu() to add additional links and other output
function new_nav_menu_items($items) {
	$homelink = '&lt;li class=&quot;home&quot;&gt;&lt;a href=&quot;' . home_url( '/' ) . '&quot;&gt;' . __('Home') . '&lt;/a&gt;&lt;/li&gt;';
	$feedlink = '&lt;li class=&quot;feed&quot;&gt;&lt;a href=&quot;' . get_bloginfo_rss('rss2_url') . '&quot;&gt;' . __('RSS') . '&lt;/a&gt;&lt;/li&gt;';
	$random_text = '&lt;li class=&quot;rainbows&quot;&gt;&lt;a href=&quot;#&quot;&gt;OMG ... Double Rainbows!&lt;/a&gt;&lt;/li&gt;';
	$items = $homelink . $items;
	$items = $items . $feedlink;
	$items = $items . $random_text;
	return $items;
}
add_filter( 'wp_list_pages', 'new_nav_menu_items' );
add_filter( 'wp_nav_menu_items', 'new_nav_menu_items' );
</pre>
<p>There always seems to be some new way to add more uses to the core functions in <a href="http://wordpress.org/">WordPress</a> and I like to find, explore and write about them. I often implement them in the First Aid Theme, as I have done by using the above example code to add a &#8220;Home&#8221; link to the menu. On the next page I will leave you with one more example, the one I referred to in the opening paragraph.</p>
<p>Although this was a bit more involved it really did not make that much of a difference in the case of implementing this in a custom menu so without further ado here is the code snippet:</p>
<pre class="brush: php; title: ; notranslate">
function new_nav_menu_items($items) {
  $cat_menu_list = wp_list_categories( 'echo=0&amp;title_li=&amp;child_of=124' );
  $items = $items . $cat_menu_list;
  return $items;
}
add_filter( 'wp_nav_menu_items', 'new_nav_menu_items' );
</pre>
<div class="nota-bene">NB: Note with this example the reference to the parameter <em>&#8216;child_of=124&#8242;</em> &#8230; this is making use of some category hierarchical trickery; essentially you create a &#8220;phantom&#8221; category (note its ID to be used as the <em>&#8216;child_of&#8217;</em> value) to be the parent of the categories you want to display in your list. If, for example, you want all your categories to be listed and you do not use the &#8220;uncategorized&#8221; category then simply make all of your current &#8220;parent&#8221; categories children of the &#8220;uncategorized&#8221; category and change the code above to use <em>&#8216;child_of=1&#8242;</em>.</div>
<p>If you are trying this at home <strong>make back-ups</strong> of your files, just in case you want to revert your edits. Your category list hierarchy may need to be noted as well.</p>
<p>The key to this snippet is the <em><a href="http://codex.wordpress.org/Template_Tags/wp_list_categories">wp_list_categories()</a></em> by default will echo its results to the screen, you will have to set the parameter <em>&#8216;echo=0&#8242;</em> to turn off this default behavior.</p>
<p>As for using this technique for categories with a non-custom menu I will leave that as an exercise for those looking for a challenge &#8230; Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://wpfirstaid.com/2010/10/extend-the-wordpress-menu/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Upgrade wp_page_menu() to wp_nav_menu()</title>
		<link>http://wpfirstaid.com/2010/07/upgrade-wp_page_menu-to-wp_nav_menu/</link>
		<comments>http://wpfirstaid.com/2010/07/upgrade-wp_page_menu-to-wp_nav_menu/#comments</comments>
		<pubDate>Mon, 12 Jul 2010 23:46:26 +0000</pubDate>
		<dc:creator>The Doctor</dc:creator>
				<category><![CDATA[Functions]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[wp_nav_menu]]></category>
		<category><![CDATA[wp_page_menu]]></category>

		<guid isPermaLink="false">http://wpfirstaid.com/?p=552</guid>
		<description><![CDATA[Upgrading from wp_page_menu() to wp_nav_menu() may be as easy as a simple find and replace operation.]]></description>
			<content:encoded><![CDATA[<p>WordPress version 3.0 has been live for a while and a lot of people want to make use of the <a href="http://codex.wordpress.org/Function_Reference/wp_nav_menu"><em>wp_nav_menu()</em></a> functionality but they are using one of the older template tags: <a href="http://codex.wordpress.org/Function_Reference/wp_page_menu"><em>wp_page_menu()</em></a>; <a href="http://codex.wordpress.org/Function_Reference/wp_list_pages"><em>wp_list_pages()</em></a>; or, <a href="http://codex.wordpress.org/Template_Tags/wp_list_categories"><em>wp_list_categories()</em></a>.</p>
<p>This post will be dealing with function arguments in common between <em>wp_nav_menu()</em> and <em>wp_page_menu()</em>. Follow-up posts will be covering <a href="http://wpfirstaid.com/2010/07/upgrade-wp_list_pages-to-wp_nav_menu/"><em>wp_list_pages()</em></a> and <a href="http://wpfirstaid.com/2010/07/upgrade-wp_list_categories-to-wp_nav_menu/"><em>wp_list_categories()</em></a>.  I will not be covering the specific use for each function&#8217;s arguments; the idea is to help show how these options correlate to one another.</p>
<p>Let&#8217;s start with the full default of <em>wp_nav_menu()</em><sup> 1</sup> explicitly displayed:</p>
<pre class="brush: php; title: ; notranslate">
wp_nav_menu( array(
    'menu'              =&gt; '',
    'container'         =&gt; 'div',
    'container_class'   =&gt; '',
    'container_id'      =&gt; '',
    'menu_class'        =&gt; 'menu',
    'menu_id'           =&gt; '',
    'echo'              =&gt; true,
    'fallback_cb'       =&gt; 'wp_page_menu',
    'before'            =&gt; '',
    'after'             =&gt; '',
    'link_before'       =&gt; '',
    'link_after'        =&gt; '',
    'depth'             =&gt; 0,
    'walker'            =&gt; '',
    'theme_location'    =&gt; ''
    ) );
</pre>
<p>Followed by the same explicit defaults of <em>wp_page_menu()</em><sup> 2, 3</sup>:</p>
<pre class="brush: php; title: ; notranslate">
wp_page_menu( array(
    'sort_column'       =&gt; 'menu_order, post_title',
    'include'           =&gt; '',
    'exclude'           =&gt; '',
    'show_home'         =&gt; false,
    'menu_class'        =&gt; 'menu',
    'echo'              =&gt; true,
    'link_before'       =&gt; '',
    'link_after'        =&gt; ''
    ) );
</pre>
<p>Now onto updating <em>wp_page_menu()</em> to <em>wp_nav_menu()</em>, which turns out to be rather simple.</p>
<p>Here is the break-down of the <em>wp_page_menu()</em> default options from above:</p>
<ul>
<li><code>'sort_column'</code> &#8211; not used</li>
<li><code>'include'</code> &#8211; not used</li>
<li><code>'exclude'</code> &#8211; not used</li>
<li><code>'show_home'</code> &#8211; not used (see bonus section below)</li>
<li><code>'menu_class'</code> &#8211; same as wp_nav_menu()</li>
<li><code>'echo'</code> &#8211; same as wp_nav_menu()</li>
<li><code>'link_before'</code> &#8211; same as wp_nav_menu()</li>
<li><code>'link_after'</code> &#8211; same as wp_nav_menu()</li>
</ul>
<p>First off &#8216;sort_column&#8217;, &#8216;include&#8217;, and &#8216;exclude&#8217; are replaced by the end-user&#8217;s choices using the <abbr title="User Interface">UI</abbr> of <em>wp_nav_menu()</em> found under Appearance | Menu in the dashboard. The balance of the default options are the same in <em>wp_nav_menu()</em>, with the exception of <code>'show_home' => false</code> which is the &#8220;bonus&#8221; content.</p>
<p>Upgrading from <em>wp_page_menu()</em> to <em>wp_nav_menu()</em> may be as easy as a simple find and replace operation.</p>
<div class="bonus">
<em>Bonus</em> &#8211; Although the default in <em>wp_page_menu()</em> does not show a home page link you can add a custom menu item in <em>wp_nav_menu()</em> if you want one to appear. You may need to edit your theme to remove the orginal &#8220;home&#8221; link if one was being generated before upgrading.
</div>
<div class="nota-bene">
NB: You may also need to add this line of code to your &#8216;functions.php&#8217; template file to add custom menu support to your theme: <code>add_theme_support( 'menus' );</code>
</div>
<div class="footnote">
<ol>Resources:</p>
<li><a href="http://core.trac.wordpress.org/browser/trunk/wp-includes/nav-menu-template.php">../wp-includes/nav-menu-template.php</a></li>
<li><a href="http://core.trac.wordpress.org/browser/trunk/wp-includes/post-template.php">../wp-includes/post-template.php</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/wp_page_menu">Function Reference/wp page menu</a></li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://wpfirstaid.com/2010/07/upgrade-wp_page_menu-to-wp_nav_menu/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Going Home</title>
		<link>http://wpfirstaid.com/2009/11/going-home/</link>
		<comments>http://wpfirstaid.com/2009/11/going-home/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 20:29:29 +0000</pubDate>
		<dc:creator>The Doctor</dc:creator>
				<category><![CDATA[Functions]]></category>
		<category><![CDATA[bloginfo]]></category>
		<category><![CDATA[get_bloginfo]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[wp_list_categories]]></category>
		<category><![CDATA[wp_list_pages]]></category>
		<category><![CDATA[wp_page_menu]]></category>

		<guid isPermaLink="false">http://wpfirstaid.com/?p=5</guid>
		<description><![CDATA[Examples of adding a home page link to a WordPress theme with code that can be easily copied and pasted.]]></description>
			<content:encoded><![CDATA[<p>Creating text links back to the home page, or front page, of a blog are done in many ways. Many themes use the blog Title as a link back to the home page. It is also very common for the menu or navigation bar to link to the home page. This article will cover some examples of links for going home.</p>
<p>Let&#8217;s start with an obvious example: a link from the blog title back to the home page.</p>
<pre class="brush: php; gutter: false; title: ; notranslate">&lt;a href=&quot;http://wpfirstaid.com/&quot;&gt;WP First Aid&lt;/a&gt;</pre>
<p>The above example is very, very basic &#8230; let&#8217;s dress it up a bit:</p>
<pre class="brush: php; gutter: false; html-script: true; title: ; notranslate">&lt;h1 class=&quot;blog-title&quot;&gt;&lt;a href=&quot;http://wpfirstaid.com/&quot; alt=&quot;Going Home: WP First Aid&quot;&gt;WP First Aid&lt;/a&gt;&lt;/h1&gt;</pre>
<p>Now we have a title using the <code>h1</code> element and a CSS class of <code>blog-title</code> with some <code>alt</code> text letting the reader know where the link will be going. This is better but how does this help you? Let&#8217;s make use of WordPress&#8217; built in functions to make this example more generic and easily transferable.</p>
<pre class="brush: php; gutter: false; title: ; notranslate">&lt;h1 class=&quot;blog-title&quot;&gt;&lt;a href=&quot;&lt;?php bloginfo('url'); ?&gt;&quot; alt=&quot;Going Home: &lt;?php bloginfo('name'); ?&gt;&quot;&gt;&lt;?php bloginfo('name'); ?&gt;&lt;/a&gt;&lt;/h1&gt;</pre>
<p>This example makes use of the WordPress function <code>bloginfo()</code>. Here is a link to the <a href="http://codex.wordpress.org/Template_Tags/bloginfo">WordPress codex</a> for this function; and, here is a link to the <a href="http://codex.wordpress.org/Function_Reference/get_bloginfo"><code>get_bloginfo()</code></a> function which provides additional details.</p>
<p>Our next example is adding a home page link to a navigation bar. Many navigation bars are generated with one of three WordPress functions: <a href="http://codex.wordpress.org/wp_list_pages"><code>wp_list_pages()</code></a>, <a href="http://codex.wordpress.org/Template_Tags/wp_page_menu"><code>wp_page_menu()</code></a>, or <a href="http://codex.wordpress.org/Template_Tags/wp_list_categories"><code>wp_list_categories()</code></a>. The wp_page_menu function uses a value that creates a home page link, for example:</p>
<pre class="brush: php; gutter: false; title: ; notranslate">&lt;?php wp_page_menu('show_home=1'); ?&gt;</pre>
<p>The wp_page_menu function includes all the available parameters of the wp_list_pages function and also includes wrapping the output in an unordered list.</p>
<p>To add a home page link into a theme using the wp_list_pages function you may look at the following example for guidance:</p>
<pre class="brush: php; first-line: 10; highlight: [12]; title: ; notranslate">&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;&lt;?php bloginfo('url'); ?&gt;&quot; alt=&quot;Going Home: &lt;?php bloginfo('name'); ?&gt;&quot;&gt;&lt;?php bloginfo('name'); ?&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;?php wp_list_pages(); ?&gt;
&lt;/ul&gt;</pre>
<p>Adding a home page link to a navigation menu created with the wp_list_categories function is very similar. Let&#8217;s take a quick look at it below.</p>
<pre class="brush: php; first-line: 10; highlight: [12]; title: ; notranslate">&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;&lt;?php bloginfo('url'); ?&gt;&quot; alt=&quot;Going Home: &lt;?php bloginfo('name'); ?&gt;&quot;&gt;&lt;?php bloginfo('name'); ?&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;?php wp_list_categories(); ?&gt;
&lt;/ul&gt;</pre>
<p>The only difference in the above two examples are the function call. The links we added to the home page are almost identical to the the link example we used for the blog title. </p>
<p>Feel free to use any of the above examples in your own works. Of course, there could be additional CSS elements needed to work with your theme&#8217;s style but I will leave that as an exercise for you &#8230; or feel free to contact us with more complex code requirements.</p>
]]></content:encoded>
			<wfw:commentRss>http://wpfirstaid.com/2009/11/going-home/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

