简体   繁体   中英

How can I change absolute URL to relative in WordPress -> pagination -> previous\next_posts_link?

I have: <?php previous_posts_link(' '); ?> <?php previous_posts_link(' '); ?> -> <a href="http://example.com/path"> </a> I need: <?php previous_posts_link(' '); ?> <?php previous_posts_link(' '); ?> -> <a href="/path"> </a>

Wordpress by default uses absolute URLs.

You can create a filter that hooks to get_pagenum_link and change the link:

add_filter('get_pagenum_link', function($url) {
    $base = 'http://example.com/';
    if (0 === strpos($url, $base)) {
        $url = '/'.substr($url, strlen($base));
    }
    return $url;
});

Alternatively you can, by using an output buffer, catch the whole pages output and change links according to your needs inside the buffer. DOMDocument and DOMXPath are helpful here. Another helpful library is Net_URL2 , useful functions are parse_url and http_build_url .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM