Add html word before category

时间:2013-12-03 作者:Vektor Unbreakable

我正在尝试在类别之前添加“TupperEx”文本,但这是不可能的!大家都能帮我吗?

最后的代码是:

<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>
但我想展示:

<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>
怎么可能?

<?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>

2 个回复
最合适的回答,由SO网友:Ravinder Kumar 整理而成

如果要在每个类别名称之前添加“TupperEx”文本,可以通过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);
  });
});
代码:http://jsbin.com/AJuJeQi/1/edit?html,js,output

SO网友:Nicolai Grossherr

正如我在评论中所说的,使用get_categories, 下面是一些示例性用法。

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:

而不是使用depth 参数可以使用parent 值为的参数0, 其结果是仅显示顶层。

<小时>

2nd approach:

就像我们意识到深度参数与上述解决方案不起作用一样。在查看源代码后,很明显它不能,因为它被walk_category_tree() 在里面wp_list_categories(). 这一步不是get_categories, 因为它不构造任何输出。因此,解决您的问题的另一个合乎逻辑的解决方案是扩展Walker Class, 即Walker_Category

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 );

结束

相关推荐

Placement of categories

我已经在我的域名上安装了zAlive主题,它在幻灯片的顶部有四个选项卡。我想把这些标签放在幻灯片栏中,链接到我的不同类别,但我不知道怎么做。我的希望是,当您从幻灯片栏中选择不同的选项卡时,每个类别中相应的帖子都会显示在下面。在此方面的任何帮助都将不胜感激。