I have been trying to achieve the following without luck.
I have the next categories in wordpress:
Book (level 1)
Chapter 1 (level 2)
Article 1 (leve 3)
Article 2 (leve 3)
Article 3 (leve 3)
Chapter 2 (level 2)
Article 1 (leve 3)
Article 2 (leve 3)
Article 3 (leve 3)
All my posts has 3 categories, one for each level. What I want is get just the category on level 3 in the current post and use it in the header of my page, but also I want get the category on level 2 and use it in the sidebar.
I have tried the function get_the_categories()
, but it show me all the categories parents and childs and I cannot choose just one in a specific level.
My code is this:
function proof( $atts ) {
$categories = get_categories( array(
'orderby' => 'name',
'parent' => 0,
'hierarchical' => 0,
'depth' => 1
) );
foreach ( $categories as $category ) {
printf( '<a href="%1$s">%2$s</a><br />',
esc_url( get_category_link( $category->term_id ) ),
esc_html( $category->name )
);
}
}
add_shortcode("proof", "proof");