我需要一些自定义Wordpress导航的帮助。
我有这个助手返回一个类别列表,显示每个级别的帖子数,但仅在第三级发布链接。到目前为止,非常好。
但是,如何根据自定义帖子类型分类法重写此内容?我已经摆弄了get_terms而不是get_categories,但我得到的只是一个空白屏幕。
以下是我要重写的类别版本的代码(抱歉,它很长):
<?php
add_filter( 'cat_index', 'return_cat_index' );
function return_cat_index()
{
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC',
'parent' => 0,
'hide_empty' => false,
'exclude' => 1,
'count' => false
));
$return .= '<nav id="cat-index">';
$return .= '<ul class="menu">';
foreach ( $categories as $category ) {
$subcats = get_categories( array(
'parent' => $category->cat_ID,
'hide_empty' => false
));
//count posts in category
$count = 0;
$taxonomy = 'category';
$args = array(
'child_of' => $category->cat_ID,
);
$tax_terms = get_terms($taxonomy,$args);
foreach ($tax_terms as $tax_term) {
$count +=$tax_term->count;
}
//sprintf categories
$return .= sprintf(
'<li id="category-%1$s" class="toggle">%3$s <span class="post-count">' . $count . '</span><span class="cat-description">%5$s</span>',
$category->cat_ID,
get_category_link( $category->term_id ),
$category->name,
$category->category_count,
$category->description
);
//if subcats are available
if( $subcats ){
$return .= sprintf(
'<ul id="subCategories-%1$s">',
$category->cat_ID
);
foreach ( $subcats as $subcat ) {
$subsubcats = get_categories( array(
'parent' => $subcat->cat_ID,
'hide_empty' => false
));
//count posts in subcategory
$countsub = 0;
$taxonomy = 'category';
$args = array(
'child_of' => $subcat->cat_ID,
);
$tax_term_subs = get_terms($taxonomy,$args);
foreach ($tax_term_subs as $tax_term_sub) {
$countsub +=$tax_term_sub->count;
}
//sprintf subcats
$return .= sprintf(
'<li id="subCategory-%1$s" class="toggle">%3$s <span class="post-count">' . $countsub . '</span><span class="cat-description">%5$s</span>',
$subcat->cat_ID,
get_category_link( $subcat->cat_ID ),
$subcat->name,
$subcat->category_count,
$subcat->description
);
//if subsubcats are available
if( $subsubcats ) {
$return .= sprintf(
'<ul id="subSubCategories-%s">',
$subcat->cat_ID
);
foreach ( $subsubcats as $subsubcat ){
//sprintf subsubcats
$return .= sprintf(
'<li id="subsubCategory-%1$s" class="toggle">%3$s <span class="post-count">%4$s</span><span class="cat-description">%5$s</span>',
$subsubcat->cat_ID,
get_category_link( $subsubcat->cat_ID ),
$subsubcat->name,
$subsubcat->category_count,
$subsubcat->description
);
//print posts
$the_query = new WP_Query(array( 'post_type' => 'bestpractices', 'cat' => $subsubcat->cat_ID )); ?>
<?php if ( $the_query->have_posts() ) :
$return .= '<ul>';
while ( $the_query->have_posts() ) : $the_query->the_post();
$return .= '<li><a class="link" href="' . get_permalink() . '">' . get_the_title() . '</a></li>' . "\n";
endwhile;
$return .= '</ul>';
wp_reset_postdata();
else:
endif;
}
$return .= '</ul>'; //end subsubcat ul
}
else{} //endif subsubcats
$return .= '</li>'; //end subcat ul
}
$return .= '</ul>'; //end subcat ul
}
else{} //endif subcats
$return .= '</li>'; //end category li
} //end for each category
$return .= '</ul>';
$return .= '</nav>';
return $return;
}
?>
提前致谢,
乔治
修改 - 更新代码: 我正在从头开始重建它,解决了空白屏幕问题。但是新代码不会返回任何用于嵌套子项的term_ID。
有什么想法吗?
add_filter( 'ap14-index', 'return_ap14_index' );
function return_ap14_index()
{
$taxonomies = array(
'ap14',
);
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
'exclude' => array(),
'exclude_tree' => array(),
'include' => array(),
'number' => '',
'fields' => 'all',
'slug' => '',
'parent' => '',
'hierarchical' => true,
'child_of' => 0,
'get' => '',
'name__like' => '',
'description__like' => '',
'pad_counts' => false,
'offset' => '',
'search' => '',
'cache_domain' => 'core'
);
$terms = get_terms($taxonomies, $args);
$return .= '<ul class="menu cat-index">';
foreach ( $terms as $term ) {
$subterms = get_terms( array(
'parent' => $term->term_id,
'hide_empty' => false
));
$return .= sprintf(
'<li>%1$s',
$term->name
);
//if subterms are available
if( $subterms ){
$return .= sprintf(
'<ul id="subTerms-%1$s">',
$term->term_id
);
$return .= '</ul>'; //end subcat ul
}
$return .= '</li>'; //end category li
} //end for each category
$return .= '</ul>';
return $return;
}
编辑:再次更新,抱歉。 Term_ID现在返回正常,但子术语仍未显示。输出应该是嵌套列表,子术语显示在每个父术语li中。但他们只是没有显示,我认为这段代码不能按预期工作:
foreach ( $terms as $term ) {
$subterms = get_terms( array(
'parent' => $term->term_id,
'hide_empty' => false
));
}
这是完整的代码:
add_filter('cat_index','return_cat_index');
function return_cat_index()
{
$taxonomies = array(
'taxonomy_name',
);
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
'exclude' => array(),
'exclude_tree' => array(),
'include' => array(),
'number' => '',
'fields' => 'all',
'slug' => '',
'parent' => 0,
'hierarchical' => true,
'child_of' => 0,
'get' => '',
'name__like' => '',
'description__like' => '',
'pad_counts' => false,
'offset' => '',
'search' => '',
'cache_domain' => 'core'
);
$terms = get_terms($taxonomies, $args);
$return .= '<nav>';
$return .= '<ul>';
foreach ( $terms as $term ) {
$subterms = get_terms( array(
'parent' => $term->term_id,
'hide_empty' => false
));
//count posts in category
$count = 0;
$taxonomy = 'taxonomy_name';
$args = array(
'child_of' => $term->term_id,
);
$tax_terms = get_terms($taxonomy,$args);
foreach ($tax_terms as $tax_term) {
$count +=$tax_term->count;
}
$return .= sprintf(
'<li id="category-%1$s" class="toggle">%2$s <span class="post-count">' . $count . '</span><span class="cat-description">%3$s</span>',
$term->term_id,
$term->name,
$term->description
);
//if subcats are available
if( $subterms ){
$return .= sprintf(
'<ul id="subTerm-%1$s">',
$term->term_id
);
foreach ( $subterms as $subterm ) {
$subsubterms = get_terms( array(
'parent' => $subterm->term_ID,
'hide_empty' => false
));
//count posts in subcategory
$countsub = 0;
$taxonomy = 'taxonomy_name';
$args = array(
'child_of' => $subterm->term_id,
);
$tax_term_subs = get_terms($taxonomy,$args);
foreach ($tax_term_subs as $tax_term_sub) {
$countsub +=$tax_term_sub->count;
}
//sprintf subcats
$return .= sprintf(
'<li id="subTerm-%1$s" class="toggle">%3$s <span class="post-count">' . $countsub . '</span><span class="cat-description">%5$s</span>',
$subterm->term_id,
get_category_link( $subterm->term_id ),
$subterm->name,
$subterm->category_count,
$subterm->description
);
//if subsubcats are available
if( $subsubterms ) {
$return .= sprintf(
'<ul id="subSubTerms-%s">',
$subterm->term_id
);
foreach ( $subsubterms as $subsubterm ){
//sprintf subsubcats
$return .= sprintf(
'<li id="subsubTerm-%1$s" class="toggle">%3$s <span class="post-count">%4$s</span><span class="cat-description">%5$s</span>',
$subsubterm->term_id,
get_category_link( $subsubterm->term_id ),
$subsubterm->name,
$subsubterm->category_count,
$subsubterm->description
);
//print posts
$the_query = new WP_Query(array( 'post_type' => 'bestpractices', 'cat' => $subsubterm->term_id )); ?>
<?php if ( $the_query->have_posts() ) :
$return .= '<ul>';
while ( $the_query->have_posts() ) : $the_query->the_post();
$return .= '<li><a class="link" href="' . get_permalink() . '">' . get_the_title() . '</a></li>' . "\n";
endwhile;
$return .= '</ul>';
wp_reset_postdata();
else:
endif;
}
$return .= '</ul>'; //end subsubcat ul
}
else{} //endif subsubcats
$return .= '</li>'; //end subcat ul
}
$return .= '</ul>'; //end subcat ul
}
else{} //endif subcats
$return .= '</li>'; //end category li
} //end for each category
$return .= '</ul>';
$return .= '</nav>';
return $return;
}