我得到了一个列表类别名称的基础上,哪个标签是 Select 在WordPress,代码所有的工作和返回的类别列表,但我不能得到代码,以停止重复的类别名称.

请查看下面的代码:

<?php
    // Get the categories
    $terms = get_terms( array(
      'taxonomy' => 'category',
      'hide_empty' => 0,
      'parent' => $catparid,
    ) );

    // Loop through them
    foreach($terms as $term) {
    // Get the posts in that category with the required tag
      $args = array(
        'category_name'    => $term->name,
        'tax_query'      => array(
         array(
           'taxonomy'  => 'post_tag',
           'field'     => 'slug',
           'terms'     => $cattagfilter
         )
      )
 );

$posts_array = get_posts( $args );

     foreach ($posts_array as $value) {
        $cattest = $term->name;
        echo '<p><a href="/category/case-stuides/' .$term->slug. '">' .$term->name. '</a></p>';
        }       
      }
                                
?>

请参见下面的屏幕截图,它显示了类别名称列表中发生的事情.

enter image description here

我试过使用array_unique,但它不工作,除非我做了一些错误的事情或把它放在错误的地方,我感谢任何帮助.

推荐答案

你可以在获取项目(名称)之前使用array_unique,在获取项目(名称)之前使用foreach

<?php
$items = array("Marketing", "NHS", "Nhs", "Science");

$unique_items = array_map(function($item) {
    return strlen($item) == 3 ? strtoupper($item) : strtolower($item);
}, array_unique(array_map('strtolower', $items)));

$unique_items = array_map('ucfirst', $unique_items);

print_r($unique_items); // ["Marketing", "NHS", "Science"]

Php相关问答推荐

PHP Array to Powershell exec命令Args

从产品中获取WooCommerce产品属性单选按钮的列表WP_Query

PHP原始数据读取引发max_input_vars错误

PHP从响应应用程序json获取文件代码和URL

无法在Laravel中将日志(log)通道设置为空

WordPress:今天的计数';修改和发布的帖子

在WooCommercestore 页面上显示库存产品属性的值

curl_close()未从PHP 8开始写入CURLOPT_COOKIEJAR会话文件

HTTPPost请求在从php脚本调用时返回404,但在从node.js脚本调用时有效.终结点有效

Shopware 6插件:由于删除了配置密钥,更新后配置值不正确

execute_query 和prepare+execute 有什么区别?

如何在 PHP 中以 hh:mm:ss 格式获取两个 strtotime datetime/s 之间的时间差

如何从 URL 核心 PHP 中的 API 获取 JSON?

将自定义保存金额移至 Woocommerce 简单产品中的价格以下

如何设置Mailersend PHP API 模板变量?

如何通过额外的属性和本地化来使用 laravel 枚举

Laravel:ArgumentCountError: 函数 App\Mail 的参数太少

yii2 sql迁移覆盖新更改的记录

PHP for each 函数打印 td

在 EasyAdmin 中添加全局操作,将我的按钮放在表格下方而不是上方