博客 / WordPress 教程

通过自定义字段为自定义的taxonomy类目product-category实现排序输出

wp_list_sort() 不能直接对 get_terms() 的返回值进行排序,因为 get_terms() 返回的是 WP_Term 对象,而 order_value 是存储在 termmeta 表中的,需要手动获取。

所以

使用 get_term_meta() 获取 order_value,然后用 usort() 进行排序。

<?php
$taxonomy = 'product-category';

// 获取所有一级分类(parent = 0)
$args = array(
    'taxonomy'   => $taxonomy,
    'parent'     => 0,
    'hide_empty' => false, // 显示空分类
);

$terms = get_terms($args);

// **获取 order_value 并进行排序**
if (!empty($terms)) {
    foreach ($terms as $key => $term) {
        $order_value = get_term_meta($term->term_id, 'order_value', true);
        $terms[$key]->order_value = ($order_value !== '') ? intval($order_value) : PHP_INT_MAX; // 空值设为最大
    }

    // **使用 usort() 按 order_value 升序排序**
    usort($terms, function ($a, $b) {
        return $a->order_value <=> $b->order_value;
    });
}

// **输出分类**
if (!empty($terms)) :
    $index = 0;
    foreach ($terms as $term) :
        $index++;
        $reverseClass = ($index % 2 === 0) ? '' : 'flex-md-row-reverse';

        // 获取当前分类的产品
        $args = array(
            'post_type' => 'product',
            'tax_query' => array(
                array(
                    'taxonomy' => $taxonomy,
                    'field'    => 'term_id',
                    'terms'    => $term->term_id,
                ),
            ),
            'posts_per_page' => 6, // 限制最多 6 个产品
        );
        $products = get_posts($args);
?>

<div class="row align-items-center <?php echo esc_attr($reverseClass); ?> col-mb-30 mb-6">
    <div class="col-lg-5 <?php echo ($index % 2 === 0) ? 'pe-3 pe-md-5' : 'ps-3 ps-md-5'; ?>">
        <h3 class="text-dark h1 ls-0 mb-4"><?php echo esc_html($term->name); ?></h3>
        <p class="mb-4 fw-light lh-base op-07">
            <?php echo esc_html($term->description) ?: 'The use of different lens and sensor operating modes guarantees the best results at any time of day...'; ?>
        </p>
        <a href="<?php echo esc_url(get_term_link($term)); ?>" class="button button-small button-circle border-color button-border m-0 color h-bg-color h-text-light">
            Read More <i class="bi-arrow-right-short"></i>
        </a>
    </div>
    <div class="col-lg-7">
        <div id="oc-images" class="owl-carousel image-carousel carousel-widget" data-autoplay="4000" data-speed="1000" data-loop="true" data-nav="false" data-margin="20" data-items-xs="1" data-items-sm="3" data-items-md="4" data-items-lg="3" data-items-xl="3">
            <?php foreach ($products as $product) : ?>
                <div class="swiper-slide">
                    <a href="<?php echo esc_url(get_permalink($product->ID)); ?>" class="d-block mb-3" style="height:150px">
                        <?php 
                        $image_url = get_the_post_thumbnail_url($product->ID, 'large'); 
                        if (empty($image_url)) {
                            $image_url = get_template_directory_uri().'/images/post/1.png';
                        }
                        ?>
                        <img src="<?php echo esc_url($image_url); ?>" class="h-100 w-100 object-cover">
                    </a>
                    <a href="<?php echo esc_url(get_permalink($product->ID)); ?>">
                        <h3 class="fs-6"><?php echo esc_html($product->post_title); ?></h3>
                    </a>
                </div>
            <?php endforeach; ?>
        </div>
    </div>
</div>

<?php
    endforeach;
endif;
?>

核心逻辑说明

获取 order_value 并处理 NULL

  • 使用 get_term_meta($term->term_id, 'order_value', true) 获取 order_value
  • 如果 order_value 为空(NULL''),则设为 PHP_INT_MAX,确保它排在最后。

使用 usort() 进行排序

  • usort()order_value 进行升序排列 ($a->order_value <=> $b->order_value)。

保证 order_value 小的排在前面,空值排最后

示例排序

term_idorder_value排序结果
1011✅ 第1个
1022✅ 第2个
10399✅ 第3个
104NULL✅ 最后

这样, product-category 输出就会按照 order_value 排序了!

评论留言

您的邮箱地址不会被公开。 必填项已用 * 标注