WordPress.org

Ready to get started?Download WordPress

Forums

Category Post Count as Link (19 posts)

  1. greencode
    Member
    Posted 5 years ago #

    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)

  2. MichaelH
    Member
    Posted 5 years ago #

  3. greencode
    Member
    Posted 5 years ago #

    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!

  4. MichaelH
    Member
    Posted 5 years ago #

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

    Hate having to hack core.

  5. Crewchief
    Member
    Posted 5 years ago #

    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?

  6. greencode
    Member
    Posted 5 years ago #

    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.

  7. l053r
    Member
    Posted 5 years ago #

    hmm im too facing the same situation as Crewchief.

  8. l053r
    Member
    Posted 5 years ago #

    where is wp_list_categories located?

  9. greencode
    Member
    Posted 5 years ago #

    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.

  10. tiagopoeta
    Member
    Posted 5 years ago #

    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.

  11. greencode
    Member
    Posted 5 years ago #

    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?

  12. T4KTEEK
    Member
    Posted 5 years ago #

    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.

  13. T4KTEEK
    Member
    Posted 5 years ago #

    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.

  14. GouZi
    Member
    Posted 5 years ago #

    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.

  15. GouZi
    Member
    Posted 5 years ago #

    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.

  16. GouZi
    Member
    Posted 5 years ago #

    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);
                                            }
                                    }
  17. Marcus
    Member
    Posted 4 years ago #

    OK, I finally figured out how to do this... You have to modify core files though. So before you do it, I suggest backing up the original files related to this post (wp-includes/classes.php).

    So, to add the category count as part of the link do the following:

    1) Go to the file named classes.php in the wp-includes folder

    2) Just replace this code:

    function start_el(&$output, $category, $depth, $args) {
              extract($args);
    
              $cat_name = esc_attr( $category->name);
              $cat_name = apply_filters( 'list_cats', $cat_name, $category );
              $link = '<a href="' . get_category_link( $category->term_id ) . '" ';
              if ( $use_desc_for_title == 0 || empty($category->description) )
                  $link .= 'title="' . sprintf(__( 'View all posts filed under %s' ), $cat_name) . '"';
              else
                  $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
              $link .= '>';
              $link .= $cat_name . '</a>';
    
              if ( (! empty($feed_image)) || (! empty($feed)) ) {
                  $link .= ' ';
    
                  if ( empty($feed_image) )
                      $link .= '(';
    
                  $link .= '<a href="' . get_category_feed_link($category->term_id, $feed_type) . '"';
    
                  if ( empty($feed) )
                      $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
                  else {
                      $title = ' title="' . $feed . '"';
                      $alt = ' alt="' . $feed . '"';
                      $name = $feed;
                      $link .= $title;
                  }
    
                  $link .= '>';
    
                  if ( empty($feed_image) )
                      $link .= $name;
                  else
                      $link .= "<img src='$feed_image'$alt$title" . ' />';
                  $link .= '</a>';
                  if ( empty($feed_image) )
                      $link .= ')';
              }
    
              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);
              }
    
              if ( isset($current_category) && $current_category )
                  $_current_category = get_category( $current_category );
    
              if ( 'list' == $args['style'] ) {
                  $output .= "\t<li";
                  $class = 'cat-item cat-item-'.$category->term_id;
                  if ( isset($current_category) && $current_category && ($category->term_id == $current_category) )
                      $class .=  ' current-cat';
                  elseif ( isset($_current_category) && $_current_category && ($category->term_id == $_current_category->parent) )
                      $class .=  ' current-cat-parent';
                  $output .=  ' class="'.$class.'"';
                  $output .= ">$link\n";
              } else {
                  $output .= "\t$link<br />\n";
              }
          }

    WITH THIS:

    function start_el(&$output, $category, $depth, $args) {
              extract($args);
    
              $cat_name = esc_attr( $category->name);
              $cat_name = apply_filters( 'list_cats', $cat_name, $category );
              $link = '<a href="' . get_category_link( $category->term_id ) . '" ';
              if ( $use_desc_for_title == 0 || empty($category->description) )
                  $link .= 'title="' . sprintf(__( 'View all posts filed under %s' ), $cat_name) . '"';
              else
                  $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
              $link .= '>';
    
              if ( isset($show_count) && $show_count )
                  $link .= $cat_name . ' (' . intval($category->count) . ')</a>';
              else
                  $link .= $cat_name . '</a>';
    
              if ( (! empty($feed_image)) || (! empty($feed)) ) {
                  $link .= ' ';
    
                  if ( empty($feed_image) )
                      $link .= '(';
    
                  $link .= '<a href="' . get_category_feed_link($category->term_id, $feed_type) . '"';
    
                  if ( empty($feed) )
                      $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
                  else {
                      $title = ' title="' . $feed . '"';
                      $alt = ' alt="' . $feed . '"';
                      $name = $feed;
                      $link .= $title;
                  }
    
                  $link .= '>';
    
                  if ( empty($feed_image) )
                      $link .= $name;
                  else
                      $link .= "<img src='$feed_image'$alt$title" . ' />';
                  $link .= '</a>';
                  if ( empty($feed_image) )
                      $link .= ')';
              }
    
              if ( isset($show_date) && $show_date ) {
                  $link .= ' ' . gmdate('Y-m-d', $category->last_update_timestamp);
              }
    
              if ( isset($current_category) && $current_category )
                  $_current_category = get_category( $current_category );
    
              if ( 'list' == $args['style'] ) {
                  $output .= "\t<li";
                  $class = 'cat-item cat-item-'.$category->term_id;
                  if ( isset($current_category) && $current_category && ($category->term_id == $current_category) )
                      $class .=  ' current-cat';
                  elseif ( isset($_current_category) && $_current_category && ($category->term_id == $_current_category->parent) )
                      $class .=  ' current-cat-parent';
                  $output .=  ' class="'.$class.'"';
                  $output .= ">$link\n";
              } else {
                  $output .= "\t$link<br />\n";
              }
          }

    NOTE: what I did basically was replace the line that says $link .= $cat_name . '</a>'; with what I wanted to be there if the categories were turned on...

  18. matt-press
    Member
    Posted 4 years ago #

    this code is secure ?

  19. makingtrails
    Member
    Posted 4 years ago #

    Thanks for this. I also had the problem the post count was displaying under the category/archives. I tried hacking the code and it worked a treat but there is still a little thing that I find strange.

    I am running two sites (both WP 2.9.1) with two different themes and one has this problem and one does not, so I believe it is a fault with the theme design and not WordPress (no need to hack core files)

    The theme with the problem (Mukkamu 1.5 - http://www.wpthemesfree.com/view.php?theme_id=1899) is outdated and is probably not supported by WP 2.7/2.8+ but the theme without the problem is Blocks 2.0 (http://wordpress.org/extend/themes/blocks) a far more sophiscated theme than Mukkamu.

    Which themes/wordpress versions are you guys using?

Topic Closed

This topic has been closed to new replies.

About this Topic