• Hello,

    I’m trying to “fix” the category and archive listings to link the post count… to fix some display issues I have in my theme (a custom one).

    Can someone please tell me why this code works for categories (wp_list_categories):

    <?php
    $categories = wp_list_categories('title_li=&show_count=1&echo=0');
    $categories = ereg_replace('</a> \(([0-9]+)\)', ' <span class="count">[\\1]</span></a>', $categories);
    echo $categories;
    ?>

    But the equivalent code doesn’t work for archives (wp_get_archives):

    <?php
    $archives = wp_get_archives('type=monthly&show_post_count=1&echo=0');
    $archives = ereg_replace('</a> \(([0-9]+)\)', ' <span class="count2">[\\1]</span></a>', $archives);
    echo $archives;
    ?>

    Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    The output from wp_get_archives uses ?&nbsp; to display the space character:

    <li><a href='https://test.dev/date/2013/08/' title='August 2013'>August 2013</a>&nbsp;(1)</li>

    ereg_replace is a deprecated php function try using preg_replace:

    <?php
    $categories = wp_list_categories('title_li=&show_count=1&echo=0');
    $categories = preg_replace('/<\/a>(\s*)\(([0-9]+)\)/', ' <span class="count">[\2]</span></a>', $categories);
    echo $categories;
    ?>
    <?php
    $archives = wp_get_archives('type=monthly&show_post_count=1&echo=0');
    $archives = preg_replace('/<\/a>(.*?)\(([0-9]+)\)/', '\1<span class="count">[\2]</span></a>', $archives);
    echo $archives;
    ?>

    Thread Starter George Appiah

    (@gappiah)

    Ah, silly me… it never occurred to me to check the rendered list. Anyway, keesiemeijer, thanks for the quick fix and the tip on the deprecated function.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Category & Archive Posts Counts Linking’ is closed to new replies.