【问题标题】:How to compare an array of list of keys with an array of "cat_name" of wordpress post with array_intersect如何将键列表数组与wordpress post的“ca​​t_name”数组与array_intersect进行比较
【发布时间】:2016-12-24 06:47:01
【问题描述】:

我已经创建了这个代码:

 <?php 
             $lisections = array('anglais','boxe-thai','Cyclotourisme','Danse-modernjazz–grs–classique','danse-sportive','dessin-peinture','escrime','Espagnol','gym-tonic','gymnastique-de-detente','gymnastique-enfantine-et-sportive','Gymnastique-senior','handball','hatha-yoga','histoire-de-lart','judo - ju jitsu','Karate','kendo-iaido','natation','ninjutsu','petanque','photo-club','randonnees-pedestres','refection-fauteuils','Sportives','subaquatique','tennis-de-table','tennis-de-table–senior','Tir','gym-dentretient-top-gym','sections-arts-martiaux','sections-langues','sections-gymnastique', 'section-aquatique','section-tennisdetable' );
            foreach((get_the_category()) as $cat) { 
                                $displaycat[] = $cat->cat_name . ' '; 
             }
              //print_r($displaycat);
            $onesection=array_intersect($lisections,$displaycat);
            print_r($onesection);?>

但它不起作用。

$displaycat 显示:数组([0] => Accueil [1] => Evénements [2] => hatha-yoga [3] => Sections )。

print_r($onesection); 的结果是一个空数组而不是 Array([0] => hatha-yoga.

我的目标是获取一篇帖子的类别名称“hatha-yoga”之一,以生成一个链接以返回该部分的主页“hatha-yoga”。

【问题讨论】:

    标签: wordpress key categories array-intersect


    【解决方案1】:

    您的 $displaycat 条目有一个尾随空格 ($displaycat[] = $cat-&gt;cat_name . ' ';)。你的$lisections不要。

    试试$displaycat[] = $cat-&gt;cat_name;(没有串联)。

    【讨论】:

      猜你喜欢
      • 2021-05-18
      • 2012-10-07
      • 1970-01-01
      • 2017-12-22
      • 1970-01-01
      • 1970-01-01
      • 2010-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多