We have a Drupal 7 site where we need to add nofollow to pager links.
Currently, we are using the following code but it only adds rel="nofollow" to next pages but we need this applied to all pager links.

Please don't comment suggesting other solutions, we specifically need the nofollow attribute (no we don't want to noindex, or block with robots.txt or rel=next/prev).

Currently Using (almost works):
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'=>'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;
}

Comments

vm’s picture

question is better served in the 'module development and code questions' forum. Please edit and move your post. Thank you.

Steve Bizuns’s picture

Done!

ChengboZ’s picture

In this case, you can implement theme_pager_link (https://api.drupal.org/api/drupal/includes!pager.inc/function/theme_page...) juste like what you have done with mytheme_pager_next.

Hope this can help you.

Steve Bizuns’s picture

@ChengboZ that is what I am looking for help with, I am not able to get it to work with theme_pager_link. Do you have the exact syntax that would achieve this using theme_pager_link?

ChengboZ’s picture

@Steve Bizuns, in this case, mytheme_pager_link (mytheme is the name of your theme) in the template.php of your theme. (In fact, it is exactly the same syntax what you did with mytheme_pager_next.)

othermachines’s picture

It's pretty simple, actually.

/**
 * Preprocess variables for theme_pager_link().
 */
function MYTHEME_preprocess_pager_link(&$variables) {
  // Add rel="nofollow" to views pager links.
  $variables['attributes']['rel'] = 'nofollow';
}