• Resolved drmarkwomack

    (@drmarkwomack)


    On my Archives page, I’d like to show the category count for categories with a single post as “1 Entry” and categories with multiple posts as “X Entries”

    Here’s a link to the page http://womopage.net/sandbox/archives/

    And here’s the current code I’m using:

    <?php
    $variable = wp_list_categories('title_li=&echo=0&show_count=1');
    $variable = str_replace( '(', '<span class="cat-count">', $variable);
    $variable = str_replace( ')', ' Entries</span>', $variable);
    echo $variable;
    ?>

    How can I make this code detect categories with only one post?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter drmarkwomack

    (@drmarkwomack)

    I know I need to replace
    Entries</span>
    with a variable that returns
    Entry</span>
    when the category count=1 and
    Entries</span>
    when the count >1.

    But I don’t know PHP well enough to figure out how to make this work.

    Thread Starter drmarkwomack

    (@drmarkwomack)

    I think I can use the _n function to sort singular and plural.

    I guess that would look something like: _n( '1 Entry', '%s Entries', $count )

    But I don’t know where or how to insert that into my code.

    Thread Starter drmarkwomack

    (@drmarkwomack)

    Here’s a solution:

    <?php
    $categories = get_categories();
    echo '<ul>';
        foreach($categories as $category) {
            echo '<li><a href="' . get_category_link( $category->term_id ) . '">' . $category->name.'</a>';
            echo '<span class="cat-count"> ' . $category->count . _n(' Entry', ' Entries', $category->count ) . '</span></li>';
        }
    echo '</ul>';
    ?>

    Not ideal. This code lists categories alphabetically without preserving sub-category hierarchy. But it does sort singulars from plurals.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘List Category Count as "1 Entry"’ is closed to new replies.