Category: WordPress

WordPress Tip: How To Exclude Pages From wp_nav_menu Function

WordPress Tip: How To Exclude Pages From wp_nav_menu Function

In this tutorial, I will show you how to exclude certain pages from the navigation menu when using the wp_nav_menu() function in WordPress. The wp_nav_menu() function is usually located in the header.php file in your theme´s folder.

To my surprise, I discovered that the wp_nav_menu() does not contain “exclude” as one of its parameters. So in this case, how to prevent certain pages from showing up in the menu?

The wp_nav_menu() purpose is to display the navigation menu created in WordPress dashboard in Appearance › Menus panel if a menu is set there, and with a fall back scenario if it is not.

How to exclude pages that you don´t want to show in the Navigation Menu? This is where this tutorial helps. This tutorial applies to any WordPress theme that uses the wp_nav_menu() function.

Real Example | WordPress Twenty Eleven Theme

To give you an example. One of the popular WordPress themes is Twenty Eleven.

Below is the original code of the header.php file of the Twenty Eleven theme (Version 1.3). Many unrelated sections of the code are removed for brevity.

CODE-x: Original header.php File :: Twenty Eleven Theme (Version 1.3)

Note: To scroll within the code, you can also click on the code window and use your keyboard´s arrow keys.

<?php
/**
 * The Header for our theme.
 * Displays all of the <head> section and everything up till <div id="main">
 * @package WordPress
 * @subpackage Twenty_Eleven
 * @since Twenty Eleven 1.0
 */
?>
 
<!-- CODE REMOVED FOR BREVITY, by Boutros AbiChedid. -->
 
<?php /* Our navigation menu.  If one isn&acute;t filled out, wp_nav_menu falls back to wp_page_menu. The menu assiged to the primary position is the one used. If none is assigned, the menu with the lowest ID is used. */ ?>
 
<?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?>
                         
<!-- CODE REMOVED FOR BREVITY, by Boutros AbiChedid. -->
 
<div id="main">

CODE-x Notes:

For the purpose of this tutorial, the most important statement is line 15 of CODE-x.

The wp_nav_menu() works on WordPress 3.0 and above. I hope you will always upgrade to the latest version.

Also WordPress Twenty Ten theme (Version 1.3) uses the same wp_nav_menu() function but with different arguments. More on that later.

Code Reference: wp_nav_menu()

Result of Twenty Eleven Theme Using CODE-x

The image below, shows how the Menu appear for this Blog when I use the Twenty Eleven theme without any modifications to the wp_nav_menu():

But, I don´t Want to Show Certain Pages in the Menu. What Should I do?

To exclude certain pages from the navigation menu, you need to add the “exclude” argument to the wp_nav_menu() function as shown in the code below. This works, even though the exclude parameter is not one of the parameters listed in wp_nav_menu() function.

You have the option to exclude pages from your Menu, by changing line 15 of CODE-x. For instance, I wanted to exclude 3 pages from the menu; thus I replaced line 15 of CODE-x with the following line (Line-xM).

Line-xM

//Replace line 15 of <em>CODE-x</em> with the following line:
<?php wp_nav_menu( array( 'theme_location' => 'primary', 'exclude' => '859,889,1031') ); ?>

Note: The numbers 859, 889 and 1031, are separated by commas, and are 3 page IDs on this blog, you need to replace these numbers with yours. If you don´t know how to find the Page ID, read my previous tutorial on How To Find the Page ID.

Result of Twenty Eleven Theme When Using Line-xM

The image below, shows how the Menu appear for this Blog when I use the Twenty Eleven theme when modifying the wp_nav_menu() (using Line-xM):

If you want to exclude just one page from the Menu, then replace line 15 of CODE-x with the following line:

//Replace line 15 of <em>CODE-x</em> with the following line:
<?php wp_nav_menu( array( 'theme_location' => 'primary', 'exclude' => '859') ); ?>

I am Using WordPress Twenty Ten Theme. Can you Help me With That?

Sure I can! Open the header.php file located in your theme´s folder and find the wp_nav_menu function. For instance for Version 1.3 of Twenty Ten theme, the statement that you will be replacing is line 12 of the following code:

CODE-y: Original header.php File :: Twenty Ten Theme (Version 1.3)

<?php
/**
 * The Header for our theme.
 * Displays all of the <head> section and everything up till <div id="main">
 * @package WordPress
 * @subpackage Twenty_Ten
 * @since Twenty Ten 1.0
 */
?>
 
<!-- CODE REMOVED FOR BREVITY, by Boutros AbiChedid. -->
 
<?php wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary' ) ); ?>

 
<div id="main">

Depending of what you need to do: You have the option to exclude pages from your Menu, by changing line 12 of the CODE-y. For instance, I wanted to exclude 3 pages from the menu, then I replaced line 12 of CODE-y with the following line:

//Replace line 12 of <em>CODE-y</em> with the following line:
<?php wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary',  'exclude' => '859,889,1031')); ?>

Note: The numbers 859, 889 and 1031, are separated by commas, and are 3 page IDs on this blog, you need to replace these numbers with yours.

If you want to exclude just one page from the Menu, then replace line 12 of CODE-y with the following line

//Replace line 12 of <em>CODE-y</em> with the following line:

//Replace line 12 of <em>CODE-y</em> with the following line:
<?php wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary',  'exclude' => '859')); ?>

Note: The number 859 is a page ID on this blog, you need to replace this number with yours. If you don´t know how to find the Page ID, read this tutorial on How To Find the Page ID.

Your Turn to Talk

Now you know what to do if you want to exclude certain pages from the wp_nav_menu() output. If your theme is using the wp_nav_menu() function and you don´t want certain pages from showing up in your menu, you know what to do.

This tutorial applies to any WordPress theme that uses the wp_nav_menu() function to display the navigation menu. The exclude parameter can still be used, even though it is not listed as one of the parameters in the WordPress Codex.

How To Turn Your WordPress Blog From NoFollow to DoFollow Without a Plugin

How To Turn Your WordPress Blog From NoFollow to DoFollow Without a Plugin

In this tutorial, I will show you how to easily turn your WordPress blog from the default NoFollow to a DoFollow blog without the need of a plugin. Why not turn your blog into a DoFollow blog and get more visitors, more traffic and eventualy more recognition in the Online community. Probably you already have seen some blogs displaying the DoFollow image and you wonder how it is done and what it really means. Read my next tutorial for a closer look at DoFollow versus NoFollow.

What are NoFollow and DoFollow?

DoFollow is a term that does not really exist. It is a slang term given to Websites that are not using nofollow. nofollow is a value that can be assigned to the rel attribute of the HTML anchor tag to instruct some search engines not to pass on any influence to an outbound link. The nofollow value was intended to stop comment spam in blogs. Commenting is recognized as a good way to get one way backlinks. But it has been abused and thus the creation of the nofollow value back in 2005.

You might already know that NoFollow blogs do not carry any PR value to other Websites through commenting. If your blog is DoFollow then comments made by your users get Link juice, meaning that your blog passes on the PR value to the commenter.

Should You Set Comment Links To DoFollow?

One way to encourage your readers to comment on your blog is by making your blog DoFollow. If your blog is new and you are trying to increase traffic then you need to turn your blog to a DoFollow blog. When people comment on your content they are adding value to your content. Some consider DoFollow as bribing visitors for comments, while others consider it as rewarding visitors that leave a comment. I tend to agree with the latter, but whatever your opinion is, DoFollow encourages interaction with your readers. One negative side though is that it increases comment moderation duties and an increase of spam comments.

What About a Plugin?

Yes, there are several plugins that turn your blog to a DoFollow blog. But I encourage you to read my previous article about unnecessarily adding plugins to your theme. Also, you really don´t need a plugin for such a simple task.

How does NoFollow and DoFollow Look Like?

The only way to know whether a link has been set to NoFollow is to look at the source code of the page. There are several ways to do that depending on the browser you use. To look at the source code, select “source” from your browser´s menu or you could right click on the page to view the source from the context menu. Also, Mozilla Firefox browser has add-ons that highlights a nofollow link on the page without actually looking at the source code.

The attribute that defines a link as NoFollow is rel=“nofollow”. If you remove the rel=“nofollow” attribute, then your link becomes DoFollow. There is no dofollow value. The absence of the nofollow value makes the link a DoFollow.

A typical DoFollow link is like this:

<a href="https://bacsoftwareconsulting.com" title="">BAC</a>

A typical NoFollow link is like this:

<a rel="nofollow" href="https://bacsoftwareconsulting.com" title="">BAC</a>

Features of My “DoFollow” Code

  1. The code is added to your theme´s functions.php file. The core WordPress code is not modified in any way.
  2. The code is split into 2 independent sections (Section1 and Section2). You have the choice to choose either one or both.
  3. The code is tested to work with the latest version of WordPress. However it also works with earlier versions.
  4. The code is tested to work properly with all major browsers.

“DoFollow” Code | functions.php

To change your Comment links from NoFollow To DoFollow, Open the functions.php file located in your theme´s folder, and add (copy and paste) the following code.

Note: To scroll within the code: You can also click on the code window and use your keyboard´s arrow keys.

<?php
/**** Section1: Changes the author's link in the comment section to a 
'dofollow' instead of the default WordPress  of 'nofollow' ****/
function dofollow_comment_author_link() {
    $url    = get_comment_author_url();
    $author = get_comment_author();
 
    if ( empty( $url ) || ('http://' == $url))
        return $author;
    else
        $link = "<a href='$url' rel='external' class='url'>$author</a>";
        return $link;
}
//http://codex.wordpress.org/Function_Reference/add_action
add_action('get_comment_author_link', 'dofollow_comment_author_link');
/* End of Section1 */
 
/**** Section2: Changes all links in the comment text to a 'dofollow' 
instead of the WordPress default of 'nofollow' ****/
function dofollow_comment_text_link($string) {
    //http://www.php.net/manual/en/function.str-ireplace.php
    $string = str_ireplace('rel="nofollow"', '', $string);
    return $string;
}
//http://codex.wordpress.org/Function_Reference/add_filter
add_filter('comment_text', 'dofollow_comment_text_link');
/* End of Section2 */
?>

“DoFollow” Code Notes:

Note: If you don´t see the horizontal bar, and to scroll within the code, you can also click on the code window and use your keyboard´s arrow keys.

1. Section1 of the above code changes the author´s link in the Comment section to a dofollow instead of the WordPress default of nofollow.

Meaning an Author´s link such as:

<a class="url" rel="external nofollow" href="https://bacsoftwareconsulting.com">BAC</a>

will be transformed to:

<a class="url" rel="external" href="https://bacsoftwareconsulting.com">BAC</a>

2. Section2 of the above code changes all links in the Comment text to a dofollow instead of the default WordPress of nofollow.

Meaning Any link in the Comment text box such as:

<a rel="nofollow" href="https://bacsoftwareconsulting.com">BAC</a>

will be transformed to:

<a href="https://bacsoftwareconsulting.com">BAC</a>

3. You have the choice to apply either Section1 of the code or Section2 or Both. For this Blog, I applied both Sections.

General Warning

When you add several PHP code blocks in your theme´s funtions.php file, make sure that you do NOT leave any white space (spaces, newline) before the opening PHP tag or after the closing PHP tag. Like so (the correct way):

<?php 
//Some Code here beetween the opening PHP tag (above) 
//and the closing PHP tag (below)...
?>
<?php 
//Some other Code here ...
?>

In the above code, if you leave any white space or a newline between lines 4 and 5, you will get the following error: Warning: Cannot modify header information – headers already sent by … , in your login screen and after you login to your WordPress dashboard.

Your Turn to Talk

What is your opinion about NoFollow or DoFollow? Does link juice mean anything to you? How easy did you find this tutorial to implement?

If you have something to say, please share your opinion in the DoFollow Comment section and get your reward. Your opinion matters, unless it is a Spam. By the way, this blog is now a DoFollow blog.