WordPress Archives Widget – Customize html output

I’m still pinned against wordpress it seems. I added the widget ‘Archives’ to my sidebar and once more, the html output is crap, it basically has this structure:

<li><a href="somelink">text</a> - (# of posts)</li>

I want to transform it into:

Read More
<li><a href="somelink">text <small># of posts</small></a>

Unlike with plugins however, I wasn’t able to find the line that creates the html output in the php pages suggested/mentioned by the wordpress community, namely functions.php, widgets.php and default-widgets.php

I’ve googled every possible keyword combination on the matter and I was unable to find something relevant.

All help is appreciated

Regards

G.Campos

Related posts

Leave a Reply

2 comments

  1. Check out general-template.php. Two functions wp_get_archives and get_archives_link. You’d have to hack wp_get_archives to change what gets loaded in $text. The post count gets loaded into the $after variable which placed outside the link in get_archives_link. Instead of this:

    $text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($arcresult->month), $arcresult->year);
    if ( $show_post_count )
       $after = '&nbsp;('.$arcresult->posts.')' . $afterafter;
    

    something like this:

    $text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($arcresult->month), $arcresult->year);
    if ( $show_post_count )
       $text= $text.'&nbsp;<small>'.$arcresult->posts.'</small>';
    

    That’s just for the Monthly archive. You’d have to make modifications on the Yearly, Weekly and Daily blocks.

    Edit: Easiest way to exclude the <small> element from the link’s title is to load it up in a separate variable in each block and then pass it into a modified get_archives_link. In the example above, right after $text gets loaded up just load that value into $title:

    $text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($arcresult->month), $arcresult->year);
    $title = $text;
    if ( $show_post_count )
       $text= $text.'&nbsp;<small>'.$arcresult->posts.'</small>';
    $output .= get_archives_link($url, $text, $format, $before, $after, $title);
    

    Then modify get_archives_link:

    function get_archives_link($url, $text, $format = 'html', $before = '', $after = '', $title = '') {
        $text = wptexturize($text);
    
        if($title == '')
            $title = $text;
    
        $title_text = esc_attr($title);
        $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>$aftern";
    
        $link_html = apply_filters( "get_archives_link", $link_html );
    
        return $link_html;
    }
    
  2. Add this code inside your theme functions.php file, It will wrap post archive counts inside span tag. In below code example I wrapped counts in span tag, you can add or modify it according to your requirement.

    function wrap_archive_count($links) {
           $links = str_replace('</a>&nbsp;(', '<span class="archive-count">', $links);
           $links = str_replace(')', '</span></a>', $links);
           return $links;
    }
    add_filter('get_archives_link', 'wrap_archive_count');