在Woocommerce中处理产品属性的术语名称并对其进行排序

用户名

我想在反向数组后按字母顺序对名称进行排序。

这是用于按正确顺序排序姓氏/名字的代码。一些错误(例如,带有中间名的名称),但除排序外,它仍然有效。

这是代码:

<?php
    $terms = get_terms( 'pa_artist' );
    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
         echo '<ul class="artists">';
        foreach ( $terms as $term ) {
            $array = explode(" ", $term->name);
            if ($array[3]) { 
                $array[3] = strtoupper($array[3]);
                $array[3] = "<strong>".$array[3]."</strong>";
            }
            elseif ($array[2]) { 
                $array[2] = strtoupper($array[2]);
                $array[2] = "<strong>".$array[2]."</strong>";
            } elseif ($array[1]) { 
                $array[1] = strtoupper($array[1]);
                $array[1] = "<strong>".$array[1]."</strong>";
            } else { 
                $array[0] = strtoupper($array[0]);
                $array[0] = "<strong>".$array[0]."</strong>";
            } 
            $rarray = array_reverse($array);
           sort($rarray); 
            echo '<li><a href="' .get_term_link( $term ). '" title="' . sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $term->name ) . '">' . implode(" ", $rarray) . '</a></li>';
        }
     echo '</ul>';
 }

现在,名称的排序就像从未进行过相反的操作一样。

一些示例,起初显示如下:

Auguste Renoir
Pablo Picasso
Paul Gauguin

在反向和If字符串之后,是这样的:

RENOIR Auguste
PICASSO Pablo
GAUGUIN Paul

当我需要它时:

GAUGUIN Paul
PICASSO Pablo
RENOIR Auguste

我尝试了各种排序功能,无法使其正常工作……我找不到在反向数组之后进行排序的方法,甚至可能吗?

它用于在wordpress / woocommerce上使用属性构建的名称列表。

我已经问过这个问题,不幸的是得到了答案……

有大约150个名称需要排序。

我愿意为此付出代价,但是没有人对此感兴趣,因为它不需要太多时间,因此不会付出太多!(仅要求重做整个网站…)

LoicTheAztec

请尝试以下操作:

$terms = get_terms( [ 'taxonomy' => 'pa_artist', 'hide_empty' => false ] );

$names_html = $terms_data = [];

if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
    // 1st Loop: Loop through the terms
    foreach ( $terms as $term ) {

        $fragments = explode( ' ', $term->name );

        if( sizeof($fragments) > 1 ) {
            // Manipulate and format the term name
            $fragment = '<strong>' . strtoupper($fragments[1]) .'</strong>';
            $new_term = $fragment . ' ' . $fragments[0];

            // 1st array: We set each formatted term name
            $names_html[] = $new_term;

            // 2nd array: We set the related data as the Url and the original term name
            $terms_data[$new_term] = array(
                'link' => get_term_link( $term ),
                'name' => $term->name,
            );
        }
    }
    // Sort the formatted term names
    sort($names_html);

    // Output
    echo '<ul class="artists">';

    // 2nd Loop: Loop through the sorted formatted term names
    foreach ( $names_html as $name_html ) {
        $link  = $terms_data[$name_html]['link'];
        $title = sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $terms_data[$name_html]['name'] );

        echo '<li><a href="' . $link . '" title="' . $title . '">' . $name_html . '</a></li>';
    }
    echo '</ul>';
}

经过测试和工作。

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从WooCommerce中的特定产品属性术语名称获取产品

在Woocommerce中显示链接的产品属性术语名称

从所有产品中获取 Woocommerce 中特定产品属性的术语名称数组

Woocommerce 正在设置术语 ID 而不是产品属性上的术语名称

在Magento中按名称对产品进行排序

使用SQL在Woocommerce中获取具有特定属性术语和特定类别术语的产品

获取按“菜单顺序”排序的WooCommerce特定产品属性术语

在管理产品常规框中获取WooCommerce产品变体属性术语

隐藏产品库中的奇数缩略图以选择特定的产品属性术语名称

在Wordpress菜单中按名称ASC对WooCommerce产品类别子菜单项进行排序

在Woocommerce中按DESC顺序对产品进行排序

在 POS (js) 中按名称对产品进行排序

在WooCommerce中通过标签获取产品属性标签名称

在Woocommerce 3中获取产品属性标签名称

获取所有与Woocommerce中与产品类别相关的术语的属性

在WooCommerce WC_Product_Query中按产品属性术语过滤

在Woocommerce中通过其产品属性子弹值对获取产品变型ID

提取列名称并在Pandas中对其进行排序

在Woocommerce档案上显示可变的产品属性和术语

在页面上显示特定WooCommerce产品属性的术语列表

显示WooCommerce产品属性的自定义分类术语图像

获取产品变体属性值的术语ID和名称

如何在 Ansible 中按其属性对列表进行排序?

根据价格范围对产品进行排序 woocommerce

列出所有WooCommerce产品属性名称

获取WooCommerce产品属性标签名称

在WooCommerce中为带有特定产品属性术语的变体制作必填订单注释字段

如何在WooCommerce中按帖子ID对类别中的产品进行排序?

显示WooCommerce自定义产品属性和单个产品上的所有术语