0

I'm looking for a way to get the values returned from wp_get_archives as an array, similar to get_categories. I need to do this to modify the post counters to use - [#] instead of (#). See my current code below:

My code for categories, which displays as desired:

$cats = get_categories();
if ($cats) {
    echo "<section class='widget'>";
    echo "<header><h4>Categories</h4></header>";
    echo "<ul>";
    foreach ($cats as $cat) {
        echo "<li><a href='" . get_category_link($cat->term_id) . "'>" . $cat->name . "</a> - [" . $cat->count . "]</li>";
    }
    echo "</ul>";
    echo "</section><!--/.widget-->";
}

My code for archvies, which does not display as desired:

$archives = wp_get_archives(array(
    "echo"            => 0,
    "show_post_count" => true,
));
if ($archives) {
    echo "<section class='widget'>";
    echo "<header><h4>Archives</h4></header>";
    echo "<ul class='cats'>" . $archives . "</ul>";
    echo "</section><!--/.widget-->";
}

1 Answer 1

2

If you look at wp_get_archives() you will notice that the link is generated by get_archives_link(). That function supplies a filter that will allow you to replace the parens.

This is fairly crude but does work.

function archive_link_wpse_183665($link) {
  $pat = '|\(([^)])\)</li>|';
  //   preg_match($pat,$link,$matches);
  //   var_dump($matches);

  $link = preg_replace($pat,'[$1]',$link);
  return $link;
}
add_filter( 'get_archives_link', 'archive_link_wpse_183665' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.