admin管理员组

文章数量:1431912

I tried to edit original the_post_navigation, inside the loop, so I could get the rel="nofollow" inside of it

the_post_navigation( array(
        'mid_size' => 3,
        'next_text' => __( 'Next frase', 'textdomain' ),
) );

I also tried to use a customized one, but that does not work.

<div class="next-timeline">
   <?php next_post_link( '%link', __( 'Próxima frase', 'adoro-frases-final' ) ); ?>
    <a href="<?php echo $permalink; ?>" rel="nofollow"><?php echo $next_post->post_title; ?></a>
</div>

I also inserted a script to the footer, so it could insert what I want into the link, but it creates another div.a instead.

$( document ).ready(function() {
        $('div.next-timeline > a#lnk-nflw').attr('rel','nofollow')
    });

Any ideas? Thanks!

I tried to edit original the_post_navigation, inside the loop, so I could get the rel="nofollow" inside of it

the_post_navigation( array(
        'mid_size' => 3,
        'next_text' => __( 'Next frase', 'textdomain' ),
) );

I also tried to use a customized one, but that does not work.

<div class="next-timeline">
   <?php next_post_link( '%link', __( 'Próxima frase', 'adoro-frases-final' ) ); ?>
    <a href="<?php echo $permalink; ?>" rel="nofollow"><?php echo $next_post->post_title; ?></a>
</div>

I also inserted a script to the footer, so it could insert what I want into the link, but it creates another div.a instead.

$( document ).ready(function() {
        $('div.next-timeline > a#lnk-nflw').attr('rel','nofollow')
    });

Any ideas? Thanks!

Share Improve this question edited Apr 13, 2019 at 2:35 nmr 4,5672 gold badges17 silver badges25 bronze badges asked Apr 12, 2019 at 18:07 OmniatomOmniatom 596 bronze badges 2
  • 2 Why would you like these links to be "nofollow"? Their main reason in there is to be followed... – Krzysiek Dróżdż Commented Apr 12, 2019 at 19:06
  • It is this project necessity. – Omniatom Commented Apr 15, 2019 at 10:43
Add a comment  | 

2 Answers 2

Reset to default 0

The only option I see is using {$adjacent}_post_link hook and replacing the fragment of final link or prepare the link yourself from scratch.

Replace:

add_filter( 'next_post_link', 'se334246_nofollow_link', 20, 5);
add_filter( 'previous_post_link', 'se334246_nofollow_link', 20, 5);

function se334246_nofollow_link( $output, $format, $link, $post, $adjacent )
{
    $search = sprintf('rel="%s"', $adjacent);
    $output = str_replace($search, 'rel="nofollow"', $output);
    return $output;
}

From scratch:

add_filter( 'next_post_link', 'se334246_nofollow_link', 20, 5);
add_filter( 'previous_post_link', 'se334246_nofollow_link', 20, 5);

function se334246_nofollow_link( $output, $format, $link, $post, $adjacent )
{
    if ( !$post )
        return $output;

    $title = $post->post_title;
    $title = apply_filters( 'the_title', $title, $post->ID );
    $permalink = get_permalink( $post );

    $inlink = str_replace( '%title', $title, $link );
    $inlink = str_replace( '%date', '', $inlink );

    $inlink = '<a href="' . $permalink . '" rel="nofollow">' . $inlink . '</a>';
    $output = str_replace( '%link', $inlink, $format );

    return $output;
}

You can filter the markup using the navigation_markup_template filter.

Just make sure the second argument passed, $class, is 'post-navigation' as the _navigation_markup() function (which generates the html) is used by 5 functions in total, each passing a different class.

本文标签: navigationHow to add nofollow to thepostnavigation