WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Rewriting Codex examples in Child Theme functions.php (7 posts)

  1. st3vi3
    Member
    Posted 3 years ago #

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

    EXAMPLE:

    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.

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

    MY UNDERSTANDING
    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!

  2. vtxyzzy
    Member
    Posted 3 years ago #

    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;
    }
    add_filter('wp_list_categories','mam_list_categories_filter');
  3. st3vi3
    Member
    Posted 3 years ago #

    hey
    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!

  4. st3vi3
    Member
    Posted 3 years ago #

    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;
    }
    add_filter('wp_list_categories','mam_list_categories_filter');

  5. vtxyzzy
    Member
    Posted 3 years ago #

    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.

  6. vtxyzzy
    Member
    Posted 3 years ago #

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

  7. st3vi3
    Member
    Posted 3 years ago #

    the css is as follows

    .post-count
    {position: relative;
    float: right;
    padding-right: 8px;}

Topic Closed

This topic has been closed to new replies.

About this Topic