Support » Fixing WordPress » Category Post Count as Link

  • Currently when WP produces the code for a Category link in the Sidebar it comes out like this:

    <li class=”cat-item cat-item-87″>Category1 (2)

    Is there any way, by tweaking the php code of the widgets.php file (I’m presuming that’s the one that needs tweaking) so that the post count is inside the link like this:

    <li class=”cat-item cat-item-87″>Category1 (2)

Viewing 15 replies - 1 through 15 (of 18 total)
  • I was hoping to get a bit of success by using a different title for the post – it seems this is one of those things that seems soooo easy but yet is impossible!

    Might need to look at the line 1317 or line 1319 in wp-includes/classes.php to do that.

    Hate having to hack core.

    When people who are not coders are seeking answers to issues they are having with WP such as this one, is there anyplace they can go to get an answer without:

    (a) Being vilified or criticized for not knowing code
    (b) receive an answer they can actually understand and implement and it works
    (c) having to read 3 to 4 posts and 2 to 3 links that lead nowhere

    This issue is a prime example… This person has made 2 posts and gotten nowhere. I’m having the exact same issue as this person. My sidebar looks like this:

    Category 1
    (3)
    Category 2
    (16)
    Category 3
    (12)

    I want it to look like this:

    Category 1 (3)
    Category 2 (16)
    Category 3 (12)

    Is there anyone on this forum who has the solution to this problem?

    Please note: I DO NOT know code

    ALL I want to do is make this one single solitary change. If you know code, please share your answer by including the following.

    (1). The exact file you are referring to, (i.e., style.css, header.php, sidebar.php, etc. ****Please don’t assume that I know which file or should know which file you are referring to; I don’t! I know of no other way to put it. And please don’t paste some code as the answer without providing any explanation.

    (2). The place in the file where the changes need to be made

    Like the person in this post, I can’t imagine that something this simple could turn out so complicated.

    This person lists a solution to the problem. but she might as well be speaking Korean: http://dev.randomland.net/2008/11/include-post-count-in-wp_list_categories-link

    There are a number of post in this forum and they all lead to nowhere.

    Does anyone have a workable solution for this?

    It does seem a very easy thing doesn’t it? And something I’m a little confused as to why WP can’t have resolved by now with their code.

    To be honest these forums are some of the worst I’ve ever come across – I’ve given up hope of ever finding a solution.

    hmm im too facing the same situation as Crewchief.

    where is wp_list_categories located?

    I’ve seen that link before and couldn’t implement the code he wrote. From what I can see though this code works perfectly if you use the Default WordPress Theme. You replace the code:

    <?php wp_list_categories(‘show_count=1&title_li=<h2>Categories</h2>’); ?>

    with

    <?php $categories = wp_list_categories(‘title_li=&show_count=1&echo=0’);
    $categories = ereg_replace(‘ \(([0-9]+)\)’, ‘ (\1)’, $categories);
    echo $categories; ?>

    in the “sidebar.php” file.

    But, if you’re using a theme which has a dynamic sidebar then there’s no place to enter this code.

    PROBLEM SOLVED:

    <?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;
    ?>

    At the previous post the </a> closing tag was cutted off by this board system.

    You say that the problem is solved but where do you enter that code you’ve mentioned? As previously mentioned if you’re using a theme with a dynamic sidebar then there’s no obvious place to enter that code.

    Any ideas?

    THIS IS THE CLOSEST I’VE GOTTEN TO FINDING A SOLUTION FOR THIS WITHOUT HAVING TO HACK CORE FILES.

    I came across this problem and could only find a CSS solution. I’ll give the reason why the (#) gets misplaced, the code itself, and the pros and cons about the solution, but first I’ll give you a working example of the solution:

    I’ve implemented this CSS code in one of my themes. You can see it working here.

    Reason for the (#) misplacement: A lot of themes use a “blocked” (display: block;) CSS style link. My theme above uses a blocked style link. Blocked style means the link will fill up one whole line of space in its container. In this case, the link fills up one whole line in the sidebar. Since the (#) isn’t part of the link it gets pushed down to the next line and you get:

    Category
    (16)
    Category
    (21)

    Now for the CSS code to fix this blocked style. ADD THIS CODE TO YOUR STYLE.CSS FILE (Anywhere is fine. I suggest the bottom though.)

    .widget_categories ul li a {
    	display: inline !important; /* cancels out blocked style links */
    	}

    Of course your sidebar CSS styles might differ so this code won’t match your sidebars link layout. You’ll notice that once you add this code to your STYLE.CSS file and save your changes. This is where you have to do some coding yourself (crewchief) by looking at how your sidebar links are styled in your STYLE.CSS file. If you don’t want to learn a little bit of coding you can always send me your style.css file and I’ll do the coding for you. I’ll also give you an explanation of what I did 🙂

    Pros: THE (#) ENDS UP WHERE IT SHOULD BE!!
    Cons: It sucks… beacause the (#) still isn’t a link! Oh well though right?

    Once again, this is the closest I’ve gotten to finding a solution without having to hack core files.

    I totally forgot to answer the TOPIC’s question and went straight to crewchief.

    In greencode’s case this doesn’t work but in crewchief’s case it does.

    Just wanted to clarify that.

    The only way I could make it work with the categories’ widget is to modify wp-includes/classes.php as following:

    --- wp-includes/classes.php     2008-12-09 10:03:31.000000000 -0800
    +++ wp-includes/classes.php     2009-06-27 15:13:25.000000000 -0700
    @@ -1318,7 +1318,12 @@
                    else
                            $link .= 'title="' . attribute_escape( apply_filters( 'category_description', $category->description, $category )) . '"';
                    $link .= '>';
    -               $link .= $cat_name . '</a>';
    +               $link .= $cat_name;
    +
    +               if ( isset($show_count) && $show_count )
    +                       $link .= ' (' . intval($category->count) . ')';
    +
    +               $link .=  '</a>';
    
                    if ( (! empty($feed_image)) || (! empty($feed)) ) {
                            $link .= ' ';
    @@ -1348,8 +1353,8 @@
                                    $link .= ')';
                    }
    
    -               if ( isset($show_count) && $show_count )
    -                       $link .= ' (' . intval($category->count) . ')';
    +               /*if ( isset($show_count) && $show_count )
    +                       $link .= ' (' . intval($category->count) . ')'; */
    
                    if ( isset($show_date) && $show_date ) {
                            $link .= ' ' . gmdate('Y-m-d', $category->last_update_timestamp);

    Even if I don’t really like to modify the core of wordpress, that’s the only way I found.

    To do the same with the Archives, I modified wp-includes/general-template.php:

    --- wp-includes/general-template.php    2008-12-30 10:35:45.000000000 -0800
    +++ wp-includes/general-template.php    2009-06-27 15:45:52.000000000 -0700
    @@ -766,7 +766,8 @@
                                    $url = get_month_link( $arcresult->year, $arcresult->month );
                                    $text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($arcresult->month), $arcresult->year);
                                    if ( $show_post_count )
    -                                       $after = '&nbsp;('.$arcresult->posts.')' . $afterafter;
    +                                       /*$after = '&nbsp;('.$arcresult->posts.')' . $afterafter;*/
    +                                       $text .= '&nbsp;('.$arcresult->posts.')';
                                    $output .= get_archives_link($url, $text, $format, $before, $after);
                            }
                    }

    IMHO it would be nice to have the choice to select where the options have to be added (part of the link or outside…).

    I’m not sure where is the best place to ask though.

    Update for wordpress 2.8.
    Categories (mainly line offset):

    --- wp-includes/classes.php     2009-05-20 09:05:23.000000000 -0700
    +++ wp-includes/classes.php     2009-06-27 16:13:48.000000000 -0700
    @@ -1333,7 +1333,12 @@
                    else
                            $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
                    $link .= '>';
    -               $link .= $cat_name . '</a>';
    +               $link .= $cat_name;
    +
    +               if ( isset($show_count) && $show_count )
    +                       $link .= ' (' . intval($category->count) . ')';
    +
    +               $link .=  '</a>';
    
                    if ( (! empty($feed_image)) || (! empty($feed)) ) {
                            $link .= ' ';
    @@ -1363,8 +1368,8 @@
                                    $link .= ')';
                    }
    
    -               if ( isset($show_count) && $show_count )
    -                       $link .= ' (' . intval($category->count) . ')';
    +               /*if ( isset($show_count) && $show_count )
    +                       $link .= ' (' . intval($category->count) . ')'; */
    
                    if ( isset($show_date) && $show_date ) {
                            $link .= ' ' . gmdate('Y-m-d', $category->last_update_timestamp);

    Archives (more complete):

    --- wp-includes/general-template.php    2009-06-10 17:04:20.000000000 -0700
    +++ wp-includes/general-template.php    2009-06-27 16:10:28.000000000 -0700
    @@ -819,7 +819,8 @@
                                    $url = get_month_link( $arcresult->year, $arcresult->month );
                                    $text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($arcresult->month), $arcresult->year);
                                    if ( $show_post_count )
    -                                       $after = '&nbsp;('.$arcresult->posts.')' . $afterafter;
    +                                       /*$after = '&nbsp;('.$arcresult->posts.')' . $afterafter;*/
    +                                       $text .= '&nbsp;('.$arcresult->posts.')';
                                    $output .= get_archives_link($url, $text, $format, $before, $after);
                            }
                    }
    @@ -840,7 +841,8 @@
                                    $url = get_year_link($arcresult->year);
                                    $text = sprintf('%d', $arcresult->year);
                                    if ($show_post_count)
    -                                       $after = '&nbsp;('.$arcresult->posts.')' . $afterafter;
    +                                       /*$after = '&nbsp;('.$arcresult->posts.')' . $afterafter;*/
    +                                       $text .= '&nbsp;('.$arcresult->posts.')';
                                    $output .= get_archives_link($url, $text, $format, $before, $after);
                            }
                    }
    @@ -862,7 +864,8 @@
                                    $date = sprintf('%1$d-%2$02d-%3$02d 00:00:00', $arcresult->year, $arcresult->month, $arcresult->dayofmonth);
                                    $text = mysql2date($archive_day_date_format, $date);
                                    if ($show_post_count)
    -                                       $after = '&nbsp;('.$arcresult->posts.')'.$afterafter;
    +                                       /*$after = '&nbsp;('.$arcresult->posts.')'.$afterafter;*/
    +                                       $text .= '&nbsp;('.$arcresult->posts.')';
                                    $output .= get_archives_link($url, $text, $format, $before, $after);
                            }
                    }
    @@ -891,7 +894,8 @@
                                                    $url  = sprintf('%1$s/%2$s%3$sm%4$s%5$s%6$sw%7$s%8$d', get_option('home'), '', '?', '=', $arc_year, '&', '=', $arcresult->week);
                                                    $text = $arc_week_start . $archive_week_separator . $arc_week_end;
                                                    if ($show_post_count)
    -                                                       $after = '&nbsp;('.$arcresult->posts.')'.$afterafter;
    +                                                       /*$after = '&nbsp;('.$arcresult->posts.')'.$afterafter;*/
    +                                                       $text .= '&nbsp;('.$arcresult->posts.')';
                                                    $output .= get_archives_link($url, $text, $format, $before, $after);
                                            }
                                    }

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘Category Post Count as Link’ is closed to new replies.