php - 在 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
在 reverse 和 If 字符串之后,它是这样的:
RENOIR Auguste
PICASSO Pablo
GAUGUIN Paul
当我需要它时:
GAUGUIN Paul
PICASSO Pablo
RENOIR Auguste
我尝试了所有排序功能,但无法正常工作……我找不到在反向数组后排序的方法,这甚至可能吗?
它用于在 wordpress/woocommerce 上使用属性构建的名称列表。
我已经问过这个问题,不幸的是得到了没有用的答案……</p>
有 150 个名字需要订购。
我愿意为此付费,但没有人感兴趣,因为它不需要太多时间,所以不会付出太多!(只收到重做整个网站的请求……)
解决方案
尝试以下操作:
$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>';
}
测试和工作。
推荐阅读
- docker - nginx反向代理简单配置不重定向
- maven - 切换操作系统后无法生成源
- c - 在 C 中使用反射/位反转输入计算 CRC16
- php - 如何在 laravel 项目中使用 kafka 和 redis?
- office-js - Office Web 加载项无法正确加载。拒绝连接
- python - S3 boto put 后没有文件
- python - python中的KMeans(框架= django)
- asp.net - .net Core 类获取参数并以 json 形式返回
- php - PHP将带有一些(重复)元素的XML转换为Json到Json数组
- java - 在回收视图中,如何通过单击和不单击始终突出显示一个适配器项目