在父术语页上显示分类子术语(名称和图像)

时间:2018-02-05 作者:shah

我试图在它们的父术语页面上显示各自的子分类术语名称和图像。

例如:

父项AChild Term 1aChild Term 2aChild Term 3a在父项A分类法页面上,我想显示3个子项1a、2a和3a的分类法名称和图像,并将每个子项链接到各自的分类法术语页面。例如,子术语1a(名称和图像)将链接到子术语1a分类页面。

现在我可以显示子nae并链接到父页面。但希望用图像显示子名称。如果你能帮我,那就太好了。以下是我在父页中显示子级的代码:

$term_id = get_queried_object_id();
$taxonomy_name = \'post_tag\';
$term_children = get_term_children( $term_id, $taxonomy_name );

foreach ( $term_children as $child ) {
           $term = get_term_by( \'id\', $child, $taxonomy_name );
           $string .= \'<li><a href="\' . get_term_link( $child, $taxonomy_name ) . \'">\' . $term->name . \'</a></li>\';
   }
   return $string;
抱歉,我忘了在前面提到,我正在使用“分类图像”为标记添加图像。我已经更新了代码,它在父页面中显示具有名称和图像的子项。但问题是它显示所有家长的所有孩子。但我想显示特定父级的特定子级。下面是我的代码:

$terms = apply_filters( \'taxonomy-images-get-terms\', \'\',
    array(
        \'taxonomy\' => \'post_tag\',
    ) );
foreach ((array)$terms as $term) {
    $parent = $term->parent;
    if ( $parent!=\'0\') {
        $string .= \'<span class="tagbox"><a class="taglink" href="\' . esc_url( get_term_link( $term, $term->taxonomy ) ) . \'"><span class="tagname">\'.wp_get_attachment_image( $term->image_id, \'detail\' ).\'<p>\'.$term->name.\'</p></span></a><span class="tagcount">\'. $term->count .\'</span></span>\';
    }
}
return $string;
如果您能帮助我指导我如何过滤父ID以仅显示父-子详细信息(名称和图像),那将非常有用。谢谢

1 个回复
SO网友:Quang Hoang

You can try this.

$parent_terms = get_terms( array(
    \'taxonomy\'   => \'post_tag\',
    \'hide_empty\' => false,
) );

foreach ( $parent_terms as $terms) {
      $term = get_terms(array(
        \'taxonomy\'   => \'post_tag\',
        \'hide_empty\' => false,
        \'parent\'     => $terms->term_id
      ) );
      foreach ($term as $term_child){
         $string .= \'<li><a href="\' . get_term_link( $term_child->term_id ) . 
                     \'">\' . $term_child->name . \'</a></li>\';
      }
}

return $string;
结束

相关推荐

Posts list in custom taxonomy

我有这样的事情:$terms = get_the_terms( get_the_ID(), \'kosmetyki_dystrybutor\'); $terms_ids = []; foreach ( $terms as $term ) { $terms_ids[] = $term->term_id; } $args = array( \'post_type\' => \'kosmetyki\',