0

In Drupal 7 I am trying to add rel="nofollow" to ALL of the pager links in views. The closest I have got is to add the nofollow to next links, but I want it to include previous pages as well as "First" and "Last".

P.S. We specifically want the rel="nofollow" attribute to all pager links, we are not looking for other solutions (noindex, block with robots.txt, or rel=next/prev).

This is the code I am currently using:

function mytheme_pager_next($variables) {
  $text = $variables['text'];
  $element = $variables['element'];
  $interval = $variables['interval'];
  $parameters = $variables['parameters'];
  global $pager_page_array, $pager_total;
  $output = '';
  // If we are anywhere but the last page
  if ($pager_page_array[$element] < ($pager_total[$element] - 1)) {
    $page_new = pager_load_array($pager_page_array[$element] + $interval, $element, $pager_page_array);
    // If the next page is the last page, mark the link as such.
    if ($page_new[$element] == ($pager_total[$element] - 1)) {
      $output = theme('pager_last', array('text' => $text, 'element' => $element, 'parameters' => $parameters,'attributes'=>array('rel'=&gt;'nofollow')));
    }
    // The next page is not the last page.
    else {
      $output = theme('pager_link', array('text' => $text, 'page_new' => $page_new, 'element' => $element, 'parameters' => $parameters,'attributes'=>array('rel'=>'nofollow')));
    }
  }
  return $output;
}
8
  • Why? nofollow is for external links. – Jaypan Mar 28 '17 at 23:30
  • Has to do with wrong pages being indexed, I don't really want to go off topic explaining the reason. Just looking for a solution. – Haz Mar 28 '17 at 23:39
  • As far as I know, the search engines will still index pages with nofollow links. They just don't apply any SEO boost that would normally be given by such links. If you don't want the pages being indexed, you should add them to robots.txt (hence my asking 'why' - as a different solution may be preferable) – Jaypan Mar 28 '17 at 23:45
  • I don't want to debate the reason, I just want to know how to do it. For this project we cannot noindex or robots.txt block - do you know the answer? We have links to some of these pages, we will lose those links if we drop them from search. We just don't want the pagerank being split between the pages or pager numbers appearing in sitelinks. – Haz Mar 28 '17 at 23:56
  • I edited the question to clarify that we are not looking for other solutions. We are 100% sure this is the method we want to use. If you don't know the answer it's fine to not comment. – Haz Mar 29 '17 at 0:36
0

Not sure of a hook right now for something cleaner. An alternative that will allow you to do what you want,

function hook_views_post_render(&$view, &$output, &$cache) {
    dpm($output);
}

You can use a regex, something like,

(<li.*pager-item.*><a)(.*)

That will give you two groups. - Group one with every list of items and the opening tag of the anchor. - Group two with the rest of the line

You can use this to iterate over and add in between both groups,

rel="nofollow"

Of course, you post an answer and find some information. You may want to see if you find these hooks useful, https://drupal.stackexchange.com/a/140867

2
  • Thanks! (I like the idea of cleaning up the code, the snippet I had was just something I found from a similar problem). I'm not a coder but I'll see if I can adapt this in the template.php of my test site and let you know if I can get it working. I'll definitely come back and comment and mark it solved if it does the trick. I don't quite understand the syntax but I can find a similar example and try to hack it together. – Haz Mar 29 '17 at 3:44
  • Check preg_match and preg_match_all. For the dpm() to work, you'll need the devel module. You can change the word hook for your theme name, drop that in your template.php and it will display what's inside the variable $output in a nice clean manner. Hope this helps! Good luck! 👍 – rovr138 Mar 29 '17 at 3:52
0

After trying several suggestions here and on Drupal forums I found they were all overly complicated and caused the 'white screen of death. Our final solution was to simply make a minor hack to drupal core includes/pager.inc found here: http://bizuns.com/drupal/rel-nofollow-views-pager-links We backed up the file before making the change and it's working great so far, if we upgrade drupal we'll just have to make a note to modify this later. Thanks again for the feedback. If anyone has a cleaner (and simple) method I would gladly try the snippet if you provide it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.