0
我使用下面的代码来显示WordPress的帖子类别,但它输出父类别,而不是子类别。可以修改它来显示子类别吗?显示子类别而不是父类别
<div class="category">' . get_category(get_query_var('cat'))->name . '</div>
我使用下面的代码来显示WordPress的帖子类别,但它输出父类别,而不是子类别。可以修改它来显示子类别吗?显示子类别而不是父类别
<div class="category">' . get_category(get_query_var('cat'))->name . '</div>
使用此代码来获取子类的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 = ', '; }
}
}
?>
,如果你想显示的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>
Th代码的代码。这对于获取子类别列表非常有用,但我希望帖子的特定子类在图像上显示,就像日期一样。这里是我的完整代码: 'if(has_post_thumbnail($ post-> ID)) { $ image = wp_get_attachment_image_src(get_post_thumbnail_id($ post-> ID),'single-post-thumbnail'); echo'' . get_the_date('M d, y') . ' '; }' –
user3612498