2017-10-07 86 views
0

我的第一个我不是我的朋友的开发者,我可以获得所有类别ID为“1292”的子类别,但其工作正常,但我只想显示某些类别的我从类别中选择“1292”获取从父类别中选择的子类别

<?php 
$term_id = 1292; 
$taxonomy_name = 'product_cat'; 
$term_children = get_term_children($term_id, $taxonomy_name); 
foreach ($term_children as $child) { 
$term = get_term_by('id', $child, $taxonomy_name); 
$category_thumbnail = get_woocommerce_term_meta($term->term_id,  'thumbnail_id', true); 
$image = wp_get_attachment_url($category_thumbnail); 
echo '<div class="col-md-2 col-sm-12"> 

<div class="ado-prdct-cat"> 
    <div class="ado-prdct-cat-wrpr"> 
     <a href="' . get_term_link($child, $taxonomy_name) . '"> <img src="'.$image.'"> 
    </div> 

    <div class="ado-prdct-cat-ttl-wrpr"> 
    ' . $term->name . '</a> 
    </div> 
</div> 
</div>'; 
} 
?> 

类别编号1292有15个孩子类别,我希望他们中的一些由ID从父ID 1292显示页面上,如果任何人知道的最好方式同样的

回答

1

选择在哪里发生,你用硬编码的ID来使用/跳过你的代码罚款吗?

你有孩子在$子项ID,只需添加

if(in_array($child, array(123, 456))) continue; 

$term = get_term_by('id', $child, $taxonomy_name); 

跳过类别123和456,或使用!in_array只显示那些并跳过其他。 当然,您可以使用变量而不是array(123, 456)

编辑:把这个变成一个shortcode是很容易的,以及:

add_shortcode("mycategoryprinter" , function($attributes) { 
    $skipids = array(); 
    if(array_key_exists("skipids", $attributes)) { 
     $skipids = preg_split("/\s*,\s*/", $attributes["skipids"]); 
    } 
    $term_id = $attributes["termid]; 
    $taxonomy_name = $attributes["taxonomy]; 

    // code goes here, change echo to return 

}); 

然后你就可以用类似

[mycategoryprinter termid="1292" taxonomy="product_cat" skipids="123, 456"][/mycategoryprinter] 
+0

使用能否请您解释一下在哪里添加代码或修改代码以上由我发布,因为我从谷歌搜索 – user3244304

+0

替换'foreach($ term_children作为$子){'与'foreach($ term_children作为$子){ if(in_array($ child,array(123,456 ))继续;' – janh

+0

我尝试添加之前,它显示如何“语法错误意外T_CONTINUE” – user3244304