Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying add "Tuppersex" text before categories but it's impossible! Everyone can help me please?

Now, final code is this:

<ul id="cat-col-1" class="cat_col">
<li class="cat-item cat-item-815">
<a title="" href="http://www.domain.com/1/">1</a>
</li>

But i want show:

<ul id="cat-col-1" class="cat_col">
<li class="cat-item cat-item-815">
<a title="" href="http://www.domain.com/1/"> Tuppersex 1</a>
</li>

How is possible?

<?php
$get_cats = wp_list_categories( 'echo=0&title_li=&depth=1&hide_empty=0&exclude=1,762,899,951' );
$cat_array = explode('</li>',$get_cats);
$results_total = count($cat_array);
$cats_per_list = ceil($results_total / 3);
$list_number = 1;
$result_number = 0;
?>

<ul class="cat_col" id="cat-col-<?php echo $list_number; ?>">

<?php
foreach($cat_array as $category) {
$result_number++;

if($result_number % $cats_per_list == 0) {
    $list_number++;
    echo $category.'</li>
    </ul>
    <ul class="cat_col" id="cat-col-'.$list_number.'">';
}
else
    echo $category.'</li>';
}
?>
</ul>
share|improve this question

2 Answers 2

up vote 0 down vote accepted

If you want to add "Tuppersex" text before each category name, you can do it by jquery

jQuery(document).ready(function(){
  var catList = jQuery('.cat_col li a');
  catList.each(function(key, value){
    var data = jQuery(this).text();
    data = 'Tuppersex '+data;
    jQuery(this).text(data);
  });
});

Code: http://jsbin.com/AJuJeQi/1/edit?html,js,output

share|improve this answer

Like I said in the comment it's easier to do this with get_categories, below some exemplary usage.

Code:

$cat_array = get_categories('parent=0&hide_empty=0&exclude=1,762,899,951');
$results_total = count($cat_array);
$cats_per_list = ceil($results_total / 3);
$list_number = 1;
$result_number = 0;

echo '<ul class="cat_col" id="cat-col-'.$list_number.'?>">';

foreach($cat_array as $category) {
    $result_number++;
    $category_link = get_category_link( $category->term_id);
    if($result_number % $cats_per_list == 0) {
        $list_number++;
        echo '<a href="'.esc_url( $category_link ).'" title="Tuppersex">';
        echo __( 'Tuppersex', 'your-text-domain' ) . ' ' . $category->name.'</li>';
        echo '</a>';
        echo '</ul>
        <ul class="cat_col" id="cat-col-'.$list_number.'">';
    } else {
        echo '<a href="'.esc_url( $category_link ).'" title="Tuppersex">';
        echo __( 'Tuppersex', 'your-text-domain' ) . ' ' . $category->name.'</li>';
        echo '</a>';
    }
}

Edit:

Instead of using the depth parameter you can use the parent parameter with a value of 0, which has the result that only the top-level will be shown.


2nd approach:

Like we realized the depth parameter isn't working with above solution. After looking at the source it was obvious it can't, because it is used by walk_category_tree() in wp_list_categories(). That step isn't part of get_categories, because it doesn't construct any output. Because of that another logical solution for your problem is extending the Walker Class, namely the Walker_Category class.

Code:

class Custom_Cat_Walker extends Walker_Category {
    function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
        extract($args);

        $cat_name = esc_attr( $category->name );
        $cat_name = apply_filters( 'list_cats', $cat_name, $category );
        $link = '<a href="' . esc_url( get_term_link($category) ) . '" ';
        if ( $use_desc_for_title == 0 || empty($category->description) )
            $link .= 'title="' . esc_attr( 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 .= __( 'Tuppersex', 'your-text-domain' ) . ' ' . $cat_name . '</a>';

        if ( !empty($feed_image) || !empty($feed) ) {
            $link .= ' ';

            if ( empty($feed_image) )
                $link .= '(';

            $link .= '<a href="' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $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 ( !empty($show_count) )
            $link .= ' (' . intval($category->count) . ')';

        if ( 'list' == $args['style'] ) {
            $output .= "\t<li";
            $class = 'cat-item cat-item-' . $category->term_id;
            if ( !empty($current_category) ) {
                $_current_category = get_term( $current_category, $category->taxonomy );
                if ( $category->term_id == $current_category )
                    $class .=  ' current-cat';
                elseif ( $category->term_id == $_current_category->parent )
                    $class .=  ' current-cat-parent';
            }
            $output .=  ' class="' . $class . '"';
            $output .= ">$link\n";
        } else {
            $output .= "\t$link<br />\n";
        }
    }
}
array(
    'title_li' => '',
    'depth' => 1,
    'walker' => new Custom_Cat_Walker()
)
wp_list_categories( $args );
share|improve this answer
    
but htat don't work. only i want add text before category in each category li –  Vektor Unbreakable Dec 3 '13 at 20:40
    
BTW you probably should use get_categories‌​, which gives you a unformatted result to work with. @VektorUnbreakable –  ialocin Dec 3 '13 at 21:07
    
Look, with that code i have this buscatuppersex.com (see sidebar top category list). I want show Tuppersex A Coruña / Tuppersex Álava... Tuppersex before each category. @ialocin –  Vektor Unbreakable Dec 4 '13 at 0:14
    
the code works, only i want add "tuppersex" before each category @ialocin –  Vektor Unbreakable Dec 4 '13 at 0:15
    
that works but show all children categories and i only want first level. Why echo=0&title_li=&depth=1&hide_empty=0&exclude=1,762,899,951 don't work in this code? –  Vektor Unbreakable Dec 4 '13 at 1:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.