从 wp_list_categories 结构中删除锚标记的简单方法

Simple way to remove anchor tags from wp_list_categories structure

我正在使用这个简单的解决方案在存档页面上使用 wordpress 显示特定类别的所有子类别:

    <ul>
        <?php wp_list_categories( array(
            'orderby'            => 'id',
            'title_li'         => '',
            'use_desc_for_title' => false,
            'child_of'           => 15, // by industry
            'hide_empty'         => 0 
        ) ); ?>
    </ul>

问题是使用这个函数我有不需要的标记(链接)指向每个 category/subcategory 档案,这是我不想要的。是否有任何简单的解决方案可以仅删除这些链接?我尝试了 get_terms 方法,但它似乎更复杂。

您可以使用它手动循环浏览项目。

get_categories( string|array $args = '' )

来源:https://developer.wordpress.org/reference/functions/get_categories/

用法如下:

<?php
$categories = get_categories( array(
     'orderby'            => 'id',
     'title_li'         => '',
     'use_desc_for_title' => false,
     'child_of'           => 15, // by industry
     'hide_empty'         => 0 
) );
 
foreach( $categories as $category ) {
    $category_link = sprintf( 
        '<a href="%1$s" alt="%2$s">%3$s</a>',
        esc_url( get_category_link( $category->term_id ) ),
        esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ),
        esc_html( $category->name )
    );
     
    echo '<p>' . sprintf( esc_html__( 'Category: %s', 'textdomain' ), $category_link ) . '</p> ';
    echo '<p>' . sprintf( esc_html__( 'Description: %s', 'textdomain' ), $category->description ) . '</p>';
    echo '<p>' . sprintf( esc_html__( 'Post Count: %s', 'textdomain' ), $category->count ) . '</p>';
} 

现在您可以创建自己的标记了。

感谢 WP 社区的 @codex 提供此代码段。