wordpress 输出指定分类 创建分类菜单导航

在列表页输出指定分类法的terms菜单 

/**
 * 在列表页输出指定分类法的terms菜单
 * 默认输出一级类别,可指定父分类id
 *
 * $tax:	the taxonomy
 * $parent: default as 0
 * $loop_temp: 	选填单项模版id,不填直接输出链接
 * 				模版项中使用变量:$term, $term_link
 */
function x_show_tax_terms( $tax, $parent=0, $loop_temp ) {
	$args = array(
		'taxonomy'		=>	$tax,
		'hide_empty'	=> 	false,
		'parent'		=>	$parent
	);
	
	$terms = get_terms($args);
	$term_now = get_queried_object()->term_id;
	if(is_array($terms)){
		foreach ($terms as $term) {
			$term_link = get_term_link($term->term_id, $tax);
			if(!$loop_temp){
				echo '<li class="menu-item cat_';
				echo get_term_field('term_id', $term, $tax);
				if($term->term_id == $term_now){
					echo ' current-menu-item';
				}else if($term->term_id == wp_get_term_taxonomy_parent_id($term_now,$tax) || $term->term_id == wp_get_term_taxonomy_parent_id(wp_get_term_taxonomy_parent_id($term_now,$tax),$tax) ){
					echo ' current-menu-item';
				}
				echo '"><a href="'.$term_link.'">';
				echo $term->name;
				echo '</a></li> ';			
			}else{
				include get_theme_file_path( 'template-parts/loop-'.$loop_temp.'.php' );
			}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值