Archived topic
How to change links on archive pages from dofollow to nofollow
42 replies · Started by Jodie on January 30, 2023
I've now added the add-nofollow to the class list of the Headline Block in the Content Template element for displaying posts on archive pages.
When I look at the page source of one of the category pages, one post that's not supposed to have a "nofollow" attribute does have one.
When I go into the back end of that post, and check the post meta that determines whether the "nofollow" attribute gets added, it's unticked, as it should be.
Do you know why this might be happening?
Thank you so much for your help with this!
Where did you add the Snippet here?: https://generatepress.com/forums/topic/how-to-change-links-on-archive-pages-from-dofollow-to-nofollow/#post-2515041
Hi Fernando
I've taken some screenshots of what is happening when I add the snippet. It is attributing nofollow to a post that I definitely don't want to be nofollow so I took it out since we were last in touch. I've added it in again for the purpose of showing you what is happening.
I've given lots of detail below.
Thanks so much.
Cheers Jodie
Can you enable default value in the custom post meta settings?
If that doesn't work, do you have a staging site for testing?
I've enabled the default value in the custom post meta settings.
I will clear the cache on all caching plugins and Cloudflare and see what happens.
Yes, I can set up a staging site for testing.
Thanks so much for your prompt response.
I'll let you know what happens
Cheers Jodie
Alright!
Sadly Fernando enabling the default value in the custom post meta settings hasn't fixed the issue.
Can you set up a staging site? Let's check what value you post meta is returning.
Okay shall do re staging site and then I will let you know.
I will remove the script from my live site for now.
Thanks so much Fernando.
Alright! You're welcome!
Hi Fernando
I've created the staging site and added the script.
I've sent you the credentials below.
Cheers Jodie
After this line:
$boolnofollow = get_post_meta(get_the_ID(), 'add_no_follow');
Add this:
var_dump($boolnofollow);
Give these two posts different values for add_no_follow:
Chiaogoo Vs Hiyahiya Knitting Needles Reviewed
11 Best Interchangeable Knitting Needles [Recommended 2023]
Thanks so much Fernando
In the staging site I've added this var_dump($boolnofollow); to the Theme Functions PHP File
I don't know what to do when you say "Give these two posts different values for add_no_follow" what do I need to do please?
Sorry here's something else weird that I'm seeing on the staging site archive pages
It's okay, I've got it.
Try this instead:
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(), 'add_no_follow');
if($boolnofollow[0]){
$myreplace = 'href';
$myinsert = 'rel="nofollow" href';
$block_content= str_replace( $myreplace, $myinsert , $block_content );
}
}
return $block_content;
}, 10, 2 );
Let us know how it goes.