Archived topic
How to change links on archive pages from dofollow to nofollow
42 replies · Started by Jodie on January 30, 2023
Hi guys
Hope you are well.
I've set some of my posts to noindex and have now realised that those posts are coming up as dofollow links on author archive pages and post category archive pages.
I've included links for you as examples.
How can I set a nofollow link for those noindex posts displaying on archive pages?
As always I appreciate your help.
Cheers Jodie
Hi Jodie,
Add add-nofollow to the class list of the Headline Block.
Adding Custom Classes: https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/
Then, add this snippet:
add_filter( 'render_block', function( $block_content, $block ) {
if ( !is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'add-nofollow' ) !== false ) {
$myreplace = 'href';
$myinsert = 'rel="nofollow" href';
$block_content= str_replace( $myreplace, $myinsert , $block_content );
}
return $block_content;
}, 10, 2 );
Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets
Thanks so much Fernando for getting back to me.
I've created a content template generate press elements block for displaying my posts on archive pages. This content template applies automatically to every single post included in the archive.
If I add this snippet and the class to the headline block in the content template, will this make all links to the posts displayed on the archive pages "nofollow"?
Yes, it will.
Are you looking to add nofollow only to specific posts?
Yes that's exactly it!
I want to add nofollow to specific posts that are displaying on the archive pages.
If that's the case, do you have a Category for these specific posts?
If not, you can add a category for these specific posts, or you can add a custom Boolean post meta through a plugin like ACF to determine if the post would or wouldn't have nofollow.
Thanks very much for that Fernando.
If I did use ACF, what are the instructions for setting up the post meta to determine if that url is "nofollow" please?
Maybe add something like this to your Posts: https://share.getcloudapp.com/E0uRBqLj
You can then use this meta's value to determine if the code here will be ran: https://generatepress.com/forums/topic/how-to-change-links-on-archive-pages-from-dofollow-to-nofollow/#post-2514945
Thanks so much Fernando.
I will give what you suggest a try.
Thanks so much for your help.
Cheers Jodie
You're welcome!
Theoretically, your new code would look something like this:
add_filter( 'render_block', function( $block_content, $block ) {
if ( !is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'add-nofollow' ) !== false ) {
$boolnofollow = get_post_meta(get_the_ID(), 'YOUR-POST-META-FIELD-NAME');
if($boolnofollow){
$myreplace = 'href';
$myinsert = 'rel="nofollow" href';
$block_content= str_replace( $myreplace, $myinsert , $block_content );
}
}
return $block_content;
}, 10, 2 );
This line $boolnofollow = get_post_meta(get_the_ID(), 'YOUR-POST-META-FIELD-NAME'); grabs the post meta value.
This conditional statement if($boolnofollow) checks its value and runs the code if it is true.
Thanks so much Fernando
Hi Fernando
I did as you suggested and I'm now using ACF Plugin. I created the post meta and added the PHP snippet to my Theme's Function File.
And I checked in the page source in my category archive pages and my author archive page to see if the nofollow attribute was appearing for one of the posts I set as noindex. Alas no luck.
Do you mind taking a look and see if you can tell what I've done wrong?
A sincerely appreciate your help.
Cheers Jodie
Can you provide admin login credentials?
Please use the Private Information field for this: https://docs.generatepress.com/article/using-the-premium-support-forum/#private-information
Just to put things in perspective, this would already be out of our scope of support but I'll try to point you in the right direction.
Hi Fernando
Thank you very much and I understand and do so much appreciate you trying to point me in the right direction.
The details are attached.
Cheers Jodie
Have you added the code here?: https://generatepress.com/forums/topic/how-to-change-links-on-archive-pages-from-dofollow-to-nofollow/#post-2515041
Don't forget to add add-nofollow to the class list of the Headline Block.