- This topic has 42 replies, 3 voices, and was last updated 2 years, 7 months ago by
Fernando.
-
AuthorPosts
-
January 30, 2023 at 7:41 pm #2514931
Jodie
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
January 30, 2023 at 8:17 pm #2514945Fernando Customer Support
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
January 30, 2023 at 8:25 pm #2514953Jodie
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”?
January 30, 2023 at 8:26 pm #2514958Fernando Customer Support
Yes, it will.
Are you looking to add
nofollow
only to specific posts?January 30, 2023 at 8:32 pm #2514964Jodie
Yes that’s exactly it!
I want to add nofollow to specific posts that are displaying on the archive pages.
January 30, 2023 at 8:39 pm #2514967Fernando Customer Support
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
.January 30, 2023 at 8:51 pm #2514972Jodie
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?
January 30, 2023 at 10:28 pm #2515021Fernando Customer Support
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
January 30, 2023 at 10:40 pm #2515036Jodie
Thanks so much Fernando.
I will give what you suggest a try.
Thanks so much for your help.
Cheers Jodie
January 30, 2023 at 10:45 pm #2515041Fernando Customer Support
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.January 30, 2023 at 10:56 pm #2515057Jodie
Thanks so much Fernando
January 31, 2023 at 10:30 pm #2516568Jodie
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
January 31, 2023 at 10:34 pm #2516571Fernando Customer Support
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.
January 31, 2023 at 11:29 pm #2516610Jodie
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
February 1, 2023 at 1:21 am #2516682Fernando Customer Support
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. -
AuthorPosts
- You must be logged in to reply to this topic.