Site logo

[Support request] How to change links on archive pages from dofollow to nofollow

Home Forums Support [Support request] How to change links on archive pages from dofollow to nofollow

Home Forums Support How to change links on archive pages from dofollow to nofollow

Viewing 15 posts - 1 through 15 (of 43 total)
  • Author
    Posts
  • #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

    #2514945
    Fernando
    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

    #2514953
    Jodie

    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”?

    #2514958
    Fernando
    Customer Support

    Yes, it will.

    Are you looking to add nofollow only to specific posts?

    #2514964
    Jodie

    Yes that’s exactly it!

    I want to add nofollow to specific posts that are displaying on the archive pages.

    #2514967
    Fernando
    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.

    #2514972
    Jodie

    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?

    #2515021
    Fernando
    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

    #2515036
    Jodie

    Thanks so much Fernando.

    I will give what you suggest a try.

    Thanks so much for your help.

    Cheers Jodie

    #2515041
    Fernando
    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.

    #2515057
    Jodie

    Thanks so much Fernando

    #2516568
    Jodie

    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

    #2516571
    Fernando
    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.

    #2516610
    Jodie

    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

    #2516682
    Fernando
    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.

Viewing 15 posts - 1 through 15 (of 43 total)
  • You must be logged in to reply to this topic.