开发者

Wordpress Archives Widget - Customize html output

开发者 https://www.devze.com 2023-02-11 04:37 出处:网络
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:

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:

<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 combinat开发者_如何学Goion on the matter and I was unable to find something relevant.

All help is appreciated

Regards

G.Campos


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>$after\n";

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

    return $link_html;
}


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');
0

精彩评论

暂无评论...
验证码 换一张
取 消