2012-04-06 23 views
0

我需要將一個類應用到WordPress'get_archive_links函數輸出的帖子歸檔鏈接。如何將這個應用於我的主題的functions.php文件,而不是核心的WordPress文件?

$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中要做到這一點,我可以通過修改/wp-includes/general-template.php (line 842),從這個做到這一點聰明的方式,不修改核心文件,我只是不知道如何。任何指導都會很棒。

編輯:這裏是普通的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; 

}

回答

1

所以我想通了,如何做到這一點,這要歸功於this page

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的相對標籤,但你可以輕鬆修改它以添加鏈接類或其他任何東西。

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,你不應該有編輯核心文件。

未經測試..

+0

嗨馬蒂,謝謝你的回答。這看起來應該工作,但不幸的是它沒有。 – rocky 2012-04-06 23:58:06

相關問題