To get the custom field of a category, you can use the get_term_meta() function. Here's an updated code snippet that includes retrieving the custom field value for each category:

<ul>
<?php
$parent_categories = get_categories(array(
    'parent' => 0,
    'hide_empty' => false // Set to true to hide empty categories
));

foreach ($parent_categories as $category) {
    $child_categories = get_categories(array(
        'parent' => $category->term_id,
        'hide_empty' => false
    ));

    $custom_field_value = get_term_meta($category->term_id, 'custom_field_name', true); ?>

    <li <?php if (!empty($child_categories)) { echo 'class="has_sub"'; } ?>>
        <a><span><?php echo $category->name; ?></span></a>

        <?php if (!empty($child_categories)) { ?>
            <ul class="children">
                <?php foreach ($child_categories as $child_category) { ?>
                    <li><a><span><?php echo $child_category->name; ?></span></a></li>
                <?php } ?>
            </ul>
        <?php } ?>
    </li>

<?php } ?>

</ul>

Replace 'custom_field_name' with the actual name of your custom field. This code retrieves the custom field value using the get_term_meta() function and assigns it to the $custom_field_value variable. You can then use this variable as needed within the loop.

ulphp$parent_categories = get_categoriesarray parent = 0 hide_empty = false Set to true to hide empty categories;foreach $parent_categories as $category $child_categories = get_categoriesar

原文地址: https://www.cveoy.top/t/topic/i34H 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录