如何将span标记插入到wp_list_categories中

时间:2015-03-06 19:52:41

标签: wordpress

现在我在sidebar.php

中使用此功能
<?php wp_list_categories('title_li='); ?>

输出:

<li><a href="#">cat 1</a></li>
<li><a href="#">cat 2</a></li>
<li>
    <a href="#">cat 3</a>
    <ul>
        <li><a href="#">sub cat 1</a></li>
        <li><a href="#">sub cat 2</a></li>
        <li><a href="#">sub cat 3</a></li>
    </ul>
</li>

我需要的是:

<li><span></span><a href="#">cat 1</a></li>
<li><span></span><a href="#">cat 2</a></li>
<li>
    <span></span><a href="#">cat 3</a>
    <ul>
        <li><span></span><a href="#">sub cat 1</a></li>
        <li><span></span><a href="#">sub cat 2</a></li>
        <li><span></span><a href="#">sub cat 3</a></li>
    </ul>
</li>

可行吗?我不是在寻找CSS / JS解决方案。

编辑更新以指出子猫结构。

2 个答案:

答案 0 :(得分:3)

您可以通过自己构建链接来实现这一目标:

<?php
$cats = get_categories();
$output = "<ul>\n";
foreach ($cats as $cat) {
    $name = $cat->name;
    $output .= "<li><span></span><a href=\"#\">$name</a></li>\n";
}
$output .= "</ul>\n";
echo $output;
?>

如果您愿意,您也可以随时回显输出,将此代码插入到functions.php的函数中,并在任何地方调用它。

函数get_categories()获取相同的类别s wp_list_categories但不构造HTML。

编辑:您应该能够使用递归来处理子类别:

<?php
function showCats($cats) {
    $output = "<ul>\n";
    foreach ($cats as $cat) {
        $name = $cat->name;
        $output .= "<li><span></span><a href=\"#\">$name</a>";

        $children = get_categories( array('parent' => $cat->cat_ID) );
        if (count($children) > 0) {
            $output .= showCats($children);
        }

        $output .= "</li>\n";
    }
    $output .= "</ul>\n";
    return $output;
}

$cats = get_categories( array('parent' => 0) );
echo showCats($cats);
?>

答案 1 :(得分:2)

您可能还会在wp_list_categories

docs中找到方便的内容

或者,您可以尝试使用get_categories

或类似的东西:使用get_terms已编辑以包含子类别

<?php       
$cat_args = array ( 'parent' => 0 ) ;
        $parentcategories = get_terms ( "category", $cat_args ) ;
        $no_of_categories = count ( $parentcategories ) ;

    if ( $no_of_categories > 0 ) {

        foreach ( $parentcategories as $parentcategory ) {

        echo '<ul><li><span></span><a href="'.get_term_link( $parentcategory ).'">' . $parentcategory ->name . '</a>' ;

                $parent_id = $parentcategory ->term_id;
                $subcategories = get_terms ( 'category', array ( 'child_of' => $parent_id, 'hide_empty' => false ) ) ;

            foreach ( $subcategories as $subcategory ) { 

                $args = array (
                    'post_type'=> 'category',
                    'orderby'=> 'title',
                    'order'=> 'ASC',
                    'post_per_page'=> -1,
                    'nopaging'=> 'true',
                    'taxonomy_name'=> $subcategory->name
                ); 

                echo '<ul><li><span></span><a href="'.get_term_link( $subcategory ).'">' . $subcategory->name . '</a><ul>';

                    query_posts ( $args ) ;

                        while ( have_posts () ) : the_post () ;

                            ?> 
                                <li><a href="<?php the_permalink () ; ?>"><?php the_title () ; ?></a></li>
                            <?php

                        endwhile;

                    wp_reset_query () ;

                echo '</ul></li></ul>' ; 

            } 

            echo '</li></ul>' ;

            }
    }
?>
相关问题