Forum Replies Created

Viewing 4 replies - 1 through 4 (of 4 total)
  • csbartus

    (@csbartus)

    $products = get_categories('child_of=5&hide_empty=0');
        foreach ( $products as $product ) {
          echo $product->cat_name . '->' . $product->cat_ID . '->' . $product->description . ' ';
        } ?>
    csbartus

    (@csbartus)

    my bad thoughts removed … 😀

    maybe this is not the perfect topic to show my solution, but all others discussing this issue were closed

    Hi all,

    I’m a rookie in PHP, but I have some programming background. Today I’ve hacked successfully the issue of “associate an image with a category” in sidebars.
    You can check out how it works here: http://www.smuff.ro

    And there u are with the instructions:

    1. I’ve used the idea of “Category Icon Images” from http://www.nationaljoke.com/home/wordpress-plugins/. I mean the images shown for a category are .jpg files named after the category’s “nice_name”.
    2. Then I’ve hacked the /wp-includes/template-functions-category.php file’s list_cats() and wp_list_cats() function. First I’ve renamed the original functions to list_cats2() and wp_list_cats2() to keep WP original.
    3. I’ve put list_cats2() to attach image files to categories:

      $link = '|<img src="http://smuff.ro/wp-content/uploads-new/icons/'.$category->category_nicename.'.jpg" alt="" />';
    4. Then I’ve replaced the <li>, <ul> tags with delimiters like @, *, | to be able later to parse the generated result.
    5. Then I’ve put list_cats2() to return the generated string by replacing it’s default return code

      echo apply_filters('list_cats2', $thelist);

      with

      return $thelist;
    6. I’ve hacked then the sidebar.php to display my new fancy categories.

    There you are with the template functions modified:

    foreach ( (array) $categories as $category ) {
    if ( ( intval($hide_empty) == 0 || $category->category_count) && (!$hierarchical || $category->category_parent == $child_of) ) {
    $num_found++;
    $link = '|<img src="http://smuff.ro/wp-content/uploads-new/icons/'.$category->category_nicename.'.jpg" alt="" />';
    $link .= '|<a href="'.get_category_link($category->cat_ID).'" ';
    if ( $use_desc_for_title == 0 || empty($category->category_description) )
    $link .= 'title="'. sprintf(__("View all posts filed under %s"), wp_specialchars($category->cat_name)) . '"';
    else
    $link .= 'title="' . wp_specialchars(apply_filters('category_description',$category->category_description,$category)) . '"';
    $link .= '>';
    $link .= apply_filters('list_cats', $category->cat_name, $category).'</a>';
    if ( (! empty($feed_image)) || (! empty($feed)) ) {
    $link .= ' ';
    if ( empty($feed_image) )
    $link .= '(';
    $link .= '<a href="' . get_category_rss_link(0, $category->cat_ID, $category->category_nicename) . '"';
    if ( !empty($feed) ) {
    $title = ' title="' . $feed . '"';
    $alt = ' alt="' . $feed . '"';
    $name = $feed;
    $link .= $title;
    }
    $link .= '>';
    if ( !empty($feed_image) )
    $link .= "<img src='$feed_image' $alt$title" . ' />';
    else
    $link .= $name;
    $link .= '</a>';
    if (empty($feed_image))
    $link .= ')';
    }
    if ( intval($optioncount) == 1 )
    $link .= ' ('.intval($category->category_count).')|';
    if ( $optiondates ) {
    if ( $optiondates == 1 )
    $optiondates = 'Y-m-d';
    $link .= ' ' . gmdate($optiondates, $category_timestamp["$category->cat_ID"]);
    }
    if ( $list ) {
    /* $thelist .= "t<li"; */
    if (($category->cat_ID == $wp_query->get_queried_object_id()) && is_category()) {
    $thelist .= ' class="current-cat"';
    }
    /* $thelist .= ">$linkn"; */
    $thelist .= "*$link";
    } else {
    $thelist .= "t$link<br />n";
    }
    if ($hierarchical && $children)
    $thelist .= list_cats2($optionall, $all, $sort_column, $sort_order, $file, $list, $optiondates, $optioncount, $hide_empty, $use_desc_for_title, $hierarchical, $category->cat_ID, $categories, 1, $feed, $feed_image, $exclude, $hierarchical);
    if ($list)
    /* $thelist .= "</li>n"; */
    $thelist .= "**";
    }
    }
    if ( !$num_found && !$child_of ) {
    if ( $list ) {
    $before = '<li>';
    $after = '</li>';
    }
    echo $before . __("No categories") . $after . "n";
    return;
    }
    if ( $list && $child_of && $num_found && $recurse ) {
    /*
    $pre = "tt<ul class='children'>";
    $post = "tt</ul>n";
    */
    $pre = "@";
    $post = "@";
    } else {
    $pre = $post = '';
    }
    $thelist = $pre . $thelist . $post;
    if ( $recurse )
    return $thelist;
    /* echo apply_filters('list_cats2', $thelist); */
    return $thelist;
    }
    function list_cats($optionall ....

    And the code from Sidebar:

    <div>
    <h2 class="widgettitle">Catalog Produse</h2>
    <br/>
    <div class="category_table">
    <table class="category_table">
    <?php
    /* get all categories with images */
    $r = wp_list_cats2('optioncount=1&list=1&sort_column=ID');
    /* get each category w. subcategroies */
    $cats = explode('@', $r);
    /* build table */
    foreach ($cats as $c) {
    if ($c) {
    $kind = strpos(ltrim($c, "*"), "**");
    if ($kind === false) {
    /* main cat */
    $title = explode('|', $c);
    if ($title) {
    echo '<thead><tr><th colspan="2" class="category_table">' . $title[2] . '</th></tr></thead>';
    $img = array();
    $link = array();
    }
    }
    else {
    /* child cat */
    $body = explode('*', ltrim($c, "*"));
    foreach ($body as $b) {
    if ($b) {
    $t = explode('|', $b);
    $img[] = $t[1];
    $link[] = $t[2];
    } /* if */
    } /* foreach */

    /* build table cells */
    if ($img) {
    echo '<tbody><tr>';
    $i = 1;
    foreach ($img as $im) {
    echo '<td class="category_table">' . $im . '</td>';
    if (($i%2) == 0) {
    echo '</tr><tr>';
    echo '<td class="category_table">' . $link[$i-2] . '</td>';
    echo '<td class="category_table">' . $link[$i-1] . '</td>';
    echo '</tr><tr>';
    }
    $i++;
    }
    $j = count($link);
    if (($j%2) != 0) {
    echo '<td></td></tr>';
    echo '<tr><td class="category_table">' . $link[$i-2] . '</td><td></td></tr>';
    }
    echo '</tbody>';
    } /* endif img */
    } /* else */
    } /* if after foreach */
    } /*foreach */
    ?>
    </table>
    </div>
    </div>

    I hope it works also for you,
    Take care,
    cs.

Viewing 4 replies - 1 through 4 (of 4 total)