【发布时间】:2016-07-21 03:59:22
【问题描述】:
我使用下面的代码来显示 WordPress 帖子类别,但它输出的是父类别,而不是子类别。这可以修改为显示子类别吗?
<div class="category">' . get_category(get_query_var('cat'))->name . '</div>
【问题讨论】:
标签: wordpress categories
我使用下面的代码来显示 WordPress 帖子类别,但它输出的是父类别,而不是子类别。这可以修改为显示子类别吗?
<div class="category">' . get_category(get_query_var('cat'))->name . '</div>
【问题讨论】:
标签: wordpress categories
使用此代码按 ID 获取子类别
<?php $cats = get_the_category($post->ID);
$sep = '';
foreach( $cats as $cat ) {
$subcats = get_categories('child_of='.$cat->term_id);
if($subcats) {
foreach( $subcats as $subcat )
{ echo $sep . $subcat->name; $sep = ', '; }
}
}
?>
【讨论】:
if (has_post_thumbnail( $post->ID ) ) { $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); echo '<a href="' . get_the_permalink() . '"><div class="thumb" style="background: url(' . $image[0] . ') center no-repeat;"><div class="date">' . get_the_date('M d, y') . '</div></div></a>'; }
如果您想显示 WordPress 类别列表,请使用此代码
<div class="category"> <?php _e('Categories:'); ?> <?php wp_list_cats(); ?></div>
或者你想调用 woocomerce 类别然后调用使用此代码
<div class="category">
<?php
$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
echo '<br /><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>';
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats) {
foreach($sub_cats as $sub_category) {
echo $sub_category->name ;
}
}
}
}
?>
</div>
【讨论】: