匹配Wordpress PHP循环中的多属性术语
这似乎是一个足够简单的问题,但我是PHP的新手,我已经为此工作了几个小时。我在档案中循环浏览帖子,并根据某个属性为每个帖子显示不同的徽标。下面是我在functions.php中的现有函数:匹配Wordpress PHP循环中的多属性术语,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,这似乎是一个足够简单的问题,但我是PHP的新手,我已经为此工作了几个小时。我在档案中循环浏览帖子,并根据某个属性为每个帖子显示不同的徽标。下面是我在functions.php中的现有函数: function show_logo() { global $post; $attribute_names = array( 'pa_product-type' ); foreach ( $attribute_names as $attribute_name ) { $taxonomy = get
function show_logo() {
global $post;
$attribute_names = array( 'pa_product-type'
);
foreach ( $attribute_names as $attribute_name ) {
$taxonomy = get_taxonomy( $attribute_name );
if ( $taxonomy && ! is_wp_error( $taxonomy ) ) {
$terms = wp_get_post_terms( $post->ID, $attribute_name );
$terms_array = array();
if ( ! empty( $terms ) ) {
foreach ( $terms as $term ) {
if ( $term->name == 'L1' ) {
// Show L1 Logo
}
elseif ( $term->name == 'M1' ) {
// Show M1 Logo
}
elseif ( $term->name == 'H1' ) {
// Show H1 Logo
}
else {
$full_line = '<span>'. $term->name . '</span>';
}
array_push( $terms_array, $full_line );
}
echo implode( $terms_array );
}
}
}
}
函数show_logo(){
全球$员额;
$attribute\u names=数组('pa\u product-type'
);
foreach($attribute\u name作为$attribute\u name){
$taxonomy=get\u taxonomy($attribute\u name);
if($taxonomy&!is_wp_error($taxonomy)){
$terms=wp\u get\u post\u terms($post->ID,$attribute\u name);
$terms_array=array();
如果(!空($terms)){
foreach($terms作为$term){
如果($term->name=='L1'){
//显示L1标志
}
elseif($term->name=='M1'){
//显示M1标志
}
elseif($term->name=='H1'){
//显示H1标志
}
否则{
$full_line='.$term->name';
}
数组推送($terms\u数组,$full\u行);
}
回波内爆($U阵列);
}
}
}
}
如果帖子匹配多个术语(例如“L1”和“M1”),我只想显示一个不同的徽标。我尝试过很多不同的方法,但我不知道自己是否走上了正确的道路。任何帮助都将不胜感激。这应该是相当容易的,我只是不确定所有涉及的数据的完整背景 假设每篇文章只显示一个徽标,以下是一种方法: 就在
foreach($terms as$term)前面{
创建三个布尔变量:
$hasL1 = false;
$hasM1 = false;
$hasH1 = false;
然后,当其中一个术语名称匹配时,不要只显示徽标,而是将相应的变量设置为true,即$hasL1=true;
foreach完成后,在echo内爆($terms_array);
之前或之后,根据有意义的内容,设置一个新的if/elseif/else块,以确定要显示的徽标,如下所示:
if ($hasL1 && $hasM1 && $hasH1) { // Pick logo for all 3 }
elseif ($hasL1 && $hasM1) { // Pick logo for pair }
elseif ($hasL1 && $hasH1) { // Pick logo for pair }
elseif ($hasH1 && $hasM1) { // Pick logo for pair }
elseif ($hasL1) { // Pick logo }
elseif ($hasM1) { // Pick logo }
elseif ($hasH1) { // Pick logo }
else { // default logo }
当然,还有很多其他方法可以实现这一点。内部if/elseif/else中的每一个子句是否都在$full\u行中设置了一个值,以便推送到$terms\u数组中?也许在其中一个块中显示多一点代码会有助于澄清。谢谢。这非常有效。我应该早就请求帮助了。