如何在主题中自定义get_the_post_navigation

How to customize get_the_post_navigation in theme

get_the_post_navigation()函数定义在wp-includes/link-template.php

里面

如何在我的主题中覆盖它?

get_the_post_navigation 不使用任何过滤器,因此没有简单的方法来修改或覆盖其所有输出。

虽然 get_the_post_navigation 本身不应用任何过滤器,但它会调用应用过滤器的函数。具体来说 get_adjacent_post_link:

return apply_filters( "{$adjacent}_post_link", $output, $format, $link, $post );

连接到该过滤器将允许您覆盖部分(但不是全部)输出。 get_the_post_navigation 还使用 _navigation_markup,它不应用任何过滤器。不能覆盖那部分输出。

您可以 request 向该函数添加过滤器。这将是一个简单的更新,它将使您能够覆盖所有输出。

function _navigation_markup( $links, $class = 'posts-navigation', $screen_reader_text = '' ) {
    if ( empty( $screen_reader_text ) ) {
        $screen_reader_text = __( 'Posts navigation' );
    }

    $template = '
    <nav class="navigation %1$s" role="navigation">
        <h2 class="screen-reader-text">%2$s</h2>
        <div class="nav-links">%3$s</div>
    </nav>';

    //Add this line
    $template = apply_filters('navigation_markup_template', $template);

    return sprintf( $template, sanitize_html_class( $class ), esc_html( $screen_reader_text ), $links );
}

一个更简单的解决方案可能只是创建您自己的 post 导航功能,然后用您的功能替换主题中对 get_the_post_navigation 的所有引用。