0

我需要将一个类应用于 WordPressget_archive_links功能输出的帖子存档链接。我可以通过修改/wp-includes/general-template.php (line 842), 来实现这一点:

$link_html = "\t<li>$before<a href='$url' title='$title_text'>$text</a>$after</li>\n";

对此:

$link_html = "\t<li>$before<a class='hello' href='$url' title='$title_text'>$text</a>$after</li>\n";

我很确定我需要在我的主题的functions.php中添加某种过滤器来以聪明的方式完成这个,而不修改核心文件,我只是不知道如何。任何指导都会很棒。

编辑:这是来自 general-template.php 的完整的、未修改的函数:

function get_archives_link($url, $text, $format = 'html', $before = '', $after = '') {
$text = wptexturize($text);
$title_text = esc_attr($text);
$url = esc_url($url);

if ('link' == $format)
    $link_html = "\t<link rel='archives' title='$title_text' href='$url' />\n";
elseif ('option' == $format)
    $link_html = "\t<option value='$url'>$before $text $after</option>\n";
elseif ('html' == $format)
    $link_html = "\t<li>$before<a href='$url' title='$title_text'>$text</a>$after</li>\n";
else // custom
    $link_html = "\t$before<a href='$url' title='$title_text'>$text</a>$after\n";

$link_html = apply_filters( 'get_archives_link', $link_html );

return $link_html;

}

4

2 回答 2

1

所以我想出了如何做到这一点,感谢这个页面

把这个扔进去functions.php

// Filter to add nofollow attribute
function nofollow_archives($link_html) {
return str_replace('<a href=', '<a rel="nofollow" href=',  $link_html);
}

然后在任何你想要的地方调用新函数:

<?php add_filter('get_archives_link', 'nofollow_archives'); ?>
<?php wp_get_archives('type=monthly'); ?>

该示例显然显示了如何添加 nofollow rel 标签,但您可以轻松修改它以添加链接类或其他任何内容。

于 2012-04-07T05:48:03.130 回答
0

这样的事情怎么样?

function new_get_archives_link ( $link_html ) {
   if ('html' == $format) {
         $link_html = "\t<li>$before<a class='hello' href='$url' title='$title_text'>$text</a>$after</li>\n";
      }
      return $link_html;
   }
add_filter("get_archives_link", "new_get_archives_link");

将其复制到您的functions.php 中,您不必编辑核心文件。

未经测试..

于 2012-04-06T21:38:28.647 回答