The issue here is having this custom function over-write the existing values if a meta name robots index tag has already been inserted into the page.
You would need to actually add a hook into native head function.
https://developer.ww.wp.xz.cn/reference/functions/is_paged/
Try this in your child theme:
add_action('wp_head', 'meta_fix', 1);
function meta_fix() {
if(is_paged()) { echo "<meta name='robots' content='noindex, follow'/>"; }
}
Thanks for sharing your expertise Corrinarusso. That worked great!
There probably wouldn’t be a way to remove the original robots meta tag on just the paginated pages? So it doesn’t look this this where the original is listed first and then the injected afterwards?
<meta name='robots' content='index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1' />
<meta name='robots' content='noindex, follow'/>
For anyone else who might benefit from this solution and know as little as me about PHP. I wrapped the code above in a PHP tag and put it in the functions.php file in WordPress > Appearance > Theme Editor > functions.php.
So it looks like this:
<?php
add_action('wp_head', 'meta_fix', 1);
function meta_fix() {
if(is_paged()) { echo "<meta name='robots' content='noindex, follow'/>"; }
}
?>
Remember to use a child-theme.
-
This reply was modified 5 years ago by
wapoxac.
-
This reply was modified 5 years ago by
wapoxac.
For WordPress 5.7+, the following should do what you want:
function no_robots_paged($robots) {
if (is_paged()) {
$robots['index'] = $robots['follow'] = false;
$robots['noindex'] = $robots['nofollow'] = true;
}
return $robots;
}
add_filter('wp_robots', 'no_robots_paged', 999);
Please disregard wrapping it in the PHP tags above and instead follow corrinarusso’s suggestion above. I managed to get a “Cookies are blocked due to unexpected output” error when I attempted to log in to WordPress and had to edit the functions.php via FTP to get access again.