WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How can I put 'nofollow' attribute on next_post_link() ?? (13 posts)

  1. sirulian
    Member
    Posted 3 years ago #

    Hello there.
    I have a simple, basic navigation on single.php which allows the user to go to the next and previous post through the next_post_link() function.

    The code looks like this:

    <div class="singlepostnavwrap">
    <div class="singlepostnav">
    <?php previous_post_link('%link', '&laquo; PREV ARTICLE ', TRUE); ?>
    </div>
    
    <div class="singlepostnav2">
    <?php next_post_link('%link', 'NEXT ARTICLE &raquo;', TRUE); ?>
    </div>
    </div><!--//SINGLEPOSTNAVWRAP-->

    When this php is run, the html anchors output with a rel="dofollow",
    I would simply like to make these links rel="nofollow" for SEO purposes.

    How can I do this with the php links?

    Thank you so very much for your time.
    I really appreciate it! :)

  2. vtxyzzy
    Member
    Posted 3 years ago #

    You can do what you want with a filter. Add this either to your functions.php or to single.php:

    function mam_post_link_nofollow ($link) {
         global $mam_set_nofollow;
         if ($mam_set_nofollow) $link = str_replace('dofollow','nofollow',$link);
         return $link;
       }
    add_filter('previous_post_link','mam_post_link_nofollow');
    add_filter('next_post_link','mam_post_link_nofollow');

    Then set and clear the global $mam_set_nofollow like this:

    <?php $mam_set_nofollow = true; ?>
    <div class="singlepostnavwrap">
    <div class="singlepostnav">
    <?php previous_post_link('%link', '&laquo; PREV ARTICLE ', TRUE); ?>
    </div>
    
    <div class="singlepostnav2">
    <?php next_post_link('%link', 'NEXT ARTICLE &raquo;', TRUE); ?>
    </div>
    </div><!--//SINGLEPOSTNAVWRAP-->
    <?php $mam_set_nofollow = false; ?>
  3. sirulian
    Member
    Posted 3 years ago #

    Wow thank you so very much vtx.
    that is way beyond what I ever would've come up with on my own hahaha.
    Thank you so much for your help .
    butttt... maybe i did something wrong. but it's not quite working.
    take a look if you'd be so kind: http://dev.bloginity.com/blog/2010/06/01/155/

    here is my functions.php (i added in the code you supplied me):

    <?php
    
    # This theme supports gettext
    function theme_init(){
    	load_theme_textdomain('Transcript', get_template_directory() . '/languages');
    }
    add_action ('init', 'theme_init');
    
    # Define Theme Content Wrapper Width
    $GLOBALS['content_width'] = 500;
    
    # Define Includes Path */
    $gab_includes = TEMPLATEPATH . '/includes/';
    
    #this theme support post thumbnails
    add_theme_support('post-thumbnails');
    
    #This theme supports custom navigation
    if ( function_exists('wp_nav_menu') ) {
    	add_theme_support( 'nav-menus' );
    }
    
    # Load theme Javascripts into the wp_head
    require_once ($gab_includes . 'theme-js.php');
    
    # Register widget zones
    require_once ($gab_includes . 'register-widget-zones.php');
    
    # Theme Functions
    require_once ($gab_includes . 'theme-functions.php');
    
    # Theme Plugins
    require_once ($gab_includes . 'theme-plugins.php');
    
    # Theme Control Panel
    require_once ($gab_includes . 'theme-control-panel.php');
    
    # Load customized comments template
    require_once ($gab_includes . 'theme-comments.php');
    
    # Load theme thumbnails
    require_once ($gab_includes . 'theme-thumbnails.php');
    
    # Load theme variables
    require_once ($gab_includes . 'theme-variables.php');
    
    # Author 
    
    function mam_post_link_nofollow ($link) {
         global $mam_set_nofollow;
         if ($mam_set_nofollow) $link = str_replace('dofollow','nofollow',$link);
         return $link;
       }
    add_filter('previous_post_link','mam_post_link_nofollow');
    add_filter('next_post_link','mam_post_link_nofollow');    
    
    function contributors() {
    global $wpdb;
    
    	$authors = $wpdb->get_results("SELECT * from $wpdb->usermeta WHERE meta_key = 'wp_capabilities' AND meta_value = 'a:1:{s:6:\"author\";b:1;}'");
    
    	foreach ( (array) $authors as $author ) {
    
    		$author = get_userdata( $author->user_id );
    
    		$userlevel = $author->wp2_user_level;
    echo "<li>";
    echo "<a href=\"".get_bloginfo('url')."/?author=";
    echo $author->ID;
    echo "\">";
    echo get_avatar($author->ID);
    echo "</a>";
    echo '<div>';
    echo "<a href=\"".get_bloginfo('url')."/?author=";
    echo $author->ID;
    echo "\">";
    the_author_meta('display_name', $author->ID);
    echo "</a>";
    echo "</div>";
    echo "</li>";
    }
    }

    and here the relevant code i have in single.php:

    <?php get_header(); ?>
    	<?php include (TEMPLATEPATH . '/innerNarrowSidebar.php'); ?>
    	<div id="innerLeft">
    
    <?php $mam_set_nofollow = true; ?>
    <div class="singlepostnavwrap">
    <div class="singlepostnav">
    <?php previous_post_link('%link', '&laquo; PREV ARTICLE ', TRUE); ?>
    </div>
    
    <div class="singlepostnav2">
    <?php next_post_link('%link', 'NEXT ARTICLE &raquo;', TRUE); ?>
    </div>
    </div><!--//SINGLEPOSTNAVWRAP-->
    <?php $mam_set_nofollow = true; ?>
    <div id="clear"></div>
    
    		<div class="post">
    .
    .
    .

    i believe i have followed your directions correctly?
    but it is not working.
    when i view source this is what i see:

    <div class="singlepostnavwrap">
    <div class="singlepostnav">
    <a href="http://dev.bloginity.com/blog/2010/05/31/thumbnail-test/" rel="prev">&laquo; PREV ARTICLE </a></div>
    
    <div class="singlepostnav2">
    <a href="http://dev.bloginity.com/blog/2010/06/25/john-legend-musical-powerhouse-and-the-roots/" rel="next">NEXT ARTICLE &raquo;</a></div>
    </div><!--//SINGLEPOSTNAVWRAP-->

    perhaps it is because it is already calling 'rel=prev'?

    :)
    thanks so very much for your help again.

  4. vtxyzzy
    Member
    Posted 3 years ago #

    Well, you told me that the link was showing 'rel="dofollow"'. I had not seen that before, but took your word. What is actually there is 'rel="prev"' and 'rel="next"', so the function must be altered to this:

    function mam_post_link_nofollow ($link) {
         global $mam_set_nofollow;
         if ($mam_set_nofollow) $link = str_replace('prev','nofollow',$link);
         if ($mam_set_nofollow) $link = str_replace('next','nofollow',$link);
         return $link;
       }
    add_filter('previous_post_link','mam_post_link_nofollow');
    add_filter('next_post_link','mam_post_link_nofollow');
  5. sirulian
    Member
    Posted 3 years ago #

    im so sorry vtx, you're right.
    i misinformed you. it wasn't dofollow, it was prev/next.

    and it works now!!
    thank you so very much! :)
    cheers my friend-

  6. vtxyzzy
    Member
    Posted 3 years ago #

    You are welcome!

  7. vtxyzzy
    Member
    Posted 3 years ago #

    After thinking about this for a while, I realized that this could potentially cause problems if the strings 'prev' or 'next' occur more than once in the link. You should use this instead:

    function mam_post_link_nofollow ($link) {
         global $mam_set_nofollow;
         if ($mam_set_nofollow) $link = preg_replace('/rel="prev"/','rel="nofollow"',$link);
         if ($mam_set_nofollow) $link = preg_replace('/rel="next"/','rel="nofollow"',$link);
         return $link;
       }
    add_filter('previous_post_link','mam_post_link_nofollow');
    add_filter('next_post_link','mam_post_link_nofollow');
  8. elindquist
    Member
    Posted 3 years ago #

    Could a similar filter function be used to add an anchor to the end of the navigation links?

  9. vtxyzzy
    Member
    Posted 3 years ago #

    Probably, but please explain with a couple of examples.

    Also, just a tip. Normally replies to an old post will just be ignored. You should start a new thread. I just accidently saw your reply while looking for something else.

  10. elindquist
    Member
    Posted 3 years ago #

    I actually did start a thread about this, but had no responses. Thanks for answering anyways! :)

    Okay, here's the details. I've used the below code to create navigation between posts, but I need to append the #LLScontent anchor to the link so that it skips past the header.

    <div id="nav-above" class="navigation">
    <div class="nav-previous"><?php previous_post_link('&laquo; %link', '%title', TRUE); ?></div>
    <div class="nav-next"><?php next_post_link('%link &raquo;', '%title', TRUE); ?></div>
    </div><!-- end #nav-above -->

    Again, thank you!

  11. vtxyzzy
    Member
    Posted 3 years ago #

    Please post a link to that thread, and we will continue there.

  12. elindquist
    Member
    Posted 3 years ago #

  13. vtxyzzy
    Member
    Posted 3 years ago #

    Thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic