Support » Fixing WordPress » List Category Count as "1 Entry"

  • Resolved 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

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

    $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)
  • I know I need to replace
    with a variable that returns
    when the category count=1 and
    when the count >1.

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

    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.

    Here’s a solution:

    $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.