WordPress添加导航链接

WordPress默认分类只是显示名称,如下图:

修改后如下图:

修改代码如下:
1.\wp-includes\functions.php添加如下代码:

function get_url_by_name ($link , $name) {
	return "<a href='{$link}' title='{$name}'>{$name}</a>";
}
 
function get_category_path_link(){
	$category = get_the_category();
	$cat_id = $category[0]->cat_ID; 
	$home_url = get_url_by_name(home_url('/'),'首页');
	$result = "";
	while($cat_id != 0){
		$current_cat = get_category($cat_id) ;
		$current_cat_url = get_url_by_name(esc_url(get_category_link($cat_id)),$current_cat->name);
		if ($result){
			$result = "{$current_cat_url} > {$result}";     
		}else{
			$result = $current_cat_url;
		}
		$cat_id = $current_cat->category_parent;
	}
	$result = "{$home_url} > {$result}";
	return $result;
}

2.找到相应的主题\wp-content\themes\astra\inc\core\common-functions.php,该文章以astra主题为例,不同主题修改的位置和代码会有所不同;
修改astra_get_taxonomy_banner_legacy_layout函数,代码片段如下:

switch ( $meta_key ) {
	case 'archive-title':
		do_action( 'astra_before_archive_title' );						
		add_filter( 'get_the_archive_title_prefix', '__return_empty_string' );
         //注释该行
		//the_archive_title( '<h1 class="page-title ast-archive-title">', '</h1>' );
		//修改分类名
		add_filter( 'get_the_archive_title', 'get_category_path_link' );						
		//修改分类样式
		the_archive_title( '<h2 class="widget-title">', '</h2>' );
		remove_filter( 'get_the_archive_title_prefix', '__return_empty_string' );
		do_action( 'astra_after_archive_title' );							
		break;

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

Scroll to Top