注意:我'm a reasonable newcomer to .PHP, starting with this project. I understand the concept of it, but I' m还不够熟练,无法弄清楚我需要使用哪些函数来处理有点复杂的事情,例如:

Backstory:

我目前在一个Wordpress网站上工作,其中包含一系列由短代码下载的下载;代码本身几乎完全基于Easy Digital Downloads插件,添加了一些代码,以便按类别过滤下载(AJAX样式) .

这是前端用户应该看到的内容:

http://kidzneurosciencecenter.com/download-problem-example-code/

此页面上的下载由以下短代码生成:

[downloads category="bs-safetyguides"]

“bs-safetyguides”属于与各种下载相关的10个左右的其他类别slu .

问题不在于带来下载的过滤器 . 这与Easy Digital Downloads相比没有改变,效果很好 .

问题是我的修改(还有什么?),即在下载本身之上编写“所有文件/补充内容/安全循环指南”类别过滤器的代码 . 这是通过一个代码生成的,该代码检查与下载相关联的类别列表,并仅激活其各个HTML条目中直接列出其类别slug的项目 .

例如,如果下载具有以下代码: <div class= bs-safetyguides fusion-portfolio-post... ,则 bs-safetyguides 的存在将激活该slug的过滤器,从而将类别"Safe Cycling Guides"显示为前端用户的选择 .

Here is my problem:

到目前为止,我知道如何做两件事之一 .

A. 我可以编写与单个下载相关的特定类别slugs,如下所示:

<?php $individualdlcats = get_the_terms($post->ID, 'download_category'); foreach ($individualdlcats as $taxindex => $taxitem) { echo $taxitem->slug,' ';};?>

问题是,这会写下与下载项关联的所有类别 . 例如,测试页面上的"NHTSA:Fitting a Helmet"下载有两个短代码: bs-safetyguidesbs-supplemental-content . 这段代码将两个slugs回显到HTML中,例如:

<div class= bs-safetyguides bs-supplemental-content fusion-portfolio-post...

因此导致两个类别都显示在前端类别选择中,即使我通过短代码请求 - 仅按 bs-safetyguides slug分类的项目 .

B. 我可以回应我在页面上通过短代码请求的类别slu ::

$categories = explode( ',', $atts['category'] );
foreach( $categories as $category ) {
echo $category;
};

当然,这只会从我在页面上请求的短代码中写出类别slug;因此,如果我的短代码是 [downloads category="bs-safetyguides"] ,则所有下载的内容都会在其HTML中回显,无论该下载是否与之相关联 .

This is what I need to do, somehow:

我需要回应每个单独下载的唯一类别slugs,减去在shortcode中调用的slug参数中没有特别要求的任何类别slugs .

如果我知道如何通过我对.PHP的了解这样做,我会感到很沮丧 . 也许它正盯着我,我还没有意识到 - 而且它可能有,因为个别下载需要以某种方式正确编写 .

非常感谢!

EDIT:

好吧,我发现了一些可用的东西 - 但是我缺乏PHP知识对我不利 . 我正在尝试使用 array_intersect 来过滤类别,但我似乎无法传递数组 .

这就是我现在所拥有的,而且我也在没有 foreach 行的情况下进行了实验:

$individualdlcats = get_the_terms($post->ID, 'download_category'); 
foreach ($individualdlcats as $individualdlcats1 ) ;

$shortcodecatslugs = explode( ',', $atts['category'] );
foreach( $shortcodecatslugs as $shortcodecatslugs1 ) ;

$newArray = array_intersect( $individualdlcats1, $shortcodecatslugs1 );
foreach ($newArray as $key => $value) {
    echo "$value "; 
};

我几乎可以肯定我的问题与变量 $individualdlcats$shortcodecatslugs (或 $individualdlcats1$shortcodecatslugs1 ,取决于我在 array_intersect 中调用的内容)实际上并不是post slugs的纯数组,而是包含多个字符串 .

因此,我试过这个:

$individualdlcats = get_the_terms($post->ID, 'download_category'); 
foreach ($individualdlcats as $taxitem) { 
$testarrayA = $taxitem->slug;
}

$shortcodecatslugs = explode( ',', $atts['category'] );
foreach( $shortcodecatslugs as $shortcodecatslugs1 ) {
$testarrayB = $shortcodecatslugs1->slug;
}

$newArray = array_intersect($testarrayA, $testarrayB);

foreach ($newArray as $key => $value) {
    echo "$value "; 
};

这也是空洞的 . 有任何想法吗?