如何在主题中自定义 get_the_post_navigation [英] How to customize get_the_post_navigation in theme

查看:28
本文介绍了如何在主题中自定义 get_the_post_navigation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

How can I override it in my theme?

推荐答案

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

get_the_post_navigation does not utilize any filters so there is no easy way to modify or override all of its output.

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

While get_the_post_navigation does not apply any filter itself, it does call functions that do apply filter. Specifically get_adjacent_post_link:

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

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

Hooking into that filter will allow you to override some, but not all, of the output. get_the_post_navigation also uses _navigation_markup which does not apply any filters. That portion of the output cannot be overridden.

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

You could request that a filter be added to that function. It would be a simple update and it would enable you to override all of the output.

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 );
}

一个更简单的解决方案可能是创建您自己的帖子导航函数,然后用您的函数替换主题中对 get_the_post_navigation 的所有引用.

An easier solution might just be to create your own post navigation function, and then replace all references to get_the_post_navigation in your theme with your function.

这篇关于如何在主题中自定义 get_the_post_navigation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆