更改wordpress wp_get_archives函数的URL

时间:2014-01-27 17:06:23

标签: php wordpress

我在Wordpress中使用wp_get_archives功能。

<?php $args = array(
    'type'            => 'monthly',
    'limit'           => '',
    'format'          => 'html', 
    'before'          => '',
    'after'           => '',
    'show_post_count' => false,
    'echo'            => 1,
    'order'           => 'DESC'
); ?>

<?php wp_get_archives( $args ); ?> 

但是我希望能够更改URL - 而不是转到我的Wordpress安装目录,如何将其更改为我自己的自定义URL?

PS:我在general-template.php文件上找到了从line 937开始的这个函数的开始位置。

2 个答案:

答案 0 :(得分:0)

我在函数wp_get_archives中看到了链接是使用函数get_archives_link()构建的。在那里,我们找到了过滤器钩子get_archives_link,它返回档案的每个HTML链接。

因此,我们可以操纵每个字符串并替换内容:

add_filter( 'get_archives_link', function( $html ) 
{
    if( is_admin() ) // Just in case, don't run on admin side
        return $html;

    // $html is '<li><a href='http://example.com/hello-world'>Hello world!</a></li>'
    $html = str_replace( 'href="http://example.com', 'href="http://example.org', $html );
    return $html;
});

答案 1 :(得分:0)

brasofilo的解决方案对我有用,只做了一些小的调整。

之前:

$html = str_replace( 'href="http://example.com', 'href="http://example.org', $html );

后:

$html = str_replace( 'http://example.com', 'http://example.com/the_events', $html );
相关问题