Support » Fixing WordPress » Rewriting Codex examples in Child Theme functions.php

  • Resolved st3vi3


    This is probably really simple… but i’m just having trouble figuring out how to rewrite the below example taken from the Codex:


    Remove Parentheses from Category Counts

    When show_count=1, each category count is surrounded by parentheses. In order to remove the parentheses without modifying core WordPress files, use the following code.

    $variable = wp_list_categories('echo=0&show_count=1&title_li=<h2>Categories</h2>');
    $variable = str_replace(array('(',')'), '', $variable);
    echo $variable;

    so as i understand it I need to try to write a function something like this (or am i way off)?

    function XXXXXXXX {
    $variable = wp_list_categories('echo=0&show_count=1&title_li=<h2>Categories</h2>');
    $variable = str_replace(array('(',')'), '', $variable);
    echo $variable;
    add_filter ('wp_list_categories,'XXXXXXXXX");

    thanks for any help!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Filters normally get their input via parameters and return their results. I think this is what you want:

    function mam_list_categories_filter ($variable) {
       $variable = str_replace(array('(',')'), '', $variable);
       return $variable;

    thank you so much… worked perfect!
    now i know i need to get to exploring parameters some more.
    just one more thing… how could i add a style within this function
    basically to right justify the post counts?

    Many many thanks!

    ha – so i managed to find the solution

    okay – so
    if you are using a child theme – and filtering through your functions.php document this code will allow you to remove the parentheses from the post count in the wp_categories_list

    and it will also add a class “post-count” which can then be used in your style.css to re-align the count

    function mam_list_categories_filter ($variable) {
    $variable = str_replace(‘(‘, ‘<span class=”post-count”> ‘, $variable);
    $variable = str_replace(‘)’, ‘ </span>’, $variable);
    return $variable;

    Right justification will be difficult because $variable is just one long string and each category name may be a different length. You would basically have to split the string into a separate part for each category and create a two column table with the category name / link in the first column and the count in the second.

    You could add a few spaces in front of each count like this:

    $variable = str_replace(array('(',')'),array('&nbsp;&nbsp;&nbsp;',''),$variable);

    but I think that would just highlight the fact that the numbers are not aligned.

    I am curious as to the CSS you will use with the class to align the numbers.

    the css is as follows

    {position: relative;
    float: right;
    padding-right: 8px;}
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Rewriting Codex examples in Child Theme functions.php’ is closed to new replies.