Home › Forums › Back End › Use wp_list_categories to navigate through custom taxonomy to custom post type
- This topic is empty.
-
AuthorPosts
-
July 4, 2013 at 5:53 am #46106
andymacleod
ParticipantI’ve turned WordPress in an online Scheme of Work for my school. I’ve created a Custom Taxonomy called Topics, and a Custom Post Type called Lessons.
The Lessons are assigned to sub-topics like this:
Science
– A Level
— AS Physics
—Unit 1
—- Section 1
Lesson (Custom Post Type)I’ve created a taxonomy-topic.php page to use to try to navigate through to the posts… but I cannot figure out how to get to the Custom Post. I want to display only one navigation level at a time, perhaps using wp_list_categories…
I’ve read lots of pages about wp_list_categories but I cannot seem to figure this one out.
Any help would be appreciated!
Thanks,
Andy
July 4, 2013 at 11:17 am #141545andymacleod
Participant$term_slug = get_query_var( ‘term’ );
$taxonomy_name = get_query_var( ‘taxonomy’ );
$current_term = get_term_by( ‘slug’, $term_slug, $taxonomy_name );
$args_cats = array(
‘taxonomy’ => $taxonomy_name,
‘hierarchical’ => true,
‘child_of’ => $current_term->term_id,
‘depth’ => ‘1’,
‘hide_empty’ => ‘0’,
‘title_li’ => ”,
‘echo’ => ‘0’,
);
$args_children = array(
‘parent’ => $current_term->term_id,
‘hide_empty’ => false
);
$children = get_terms( $current_term->taxonomy, $args_children);
// print_r($children); // uncomment to examine for debugging
if($children) { // get_terms will return false if tax does not exist or term wasn’t found.
echo wp_list_categories($args_cats);
} else {
$args_lessons = array(
‘child_of’ => $wp_query->tax_query->queries[0],
‘title_li’ => ”,
‘post_type’ => ‘lessons’
);
wp_list_pages($args_lessons);
}?>
July 4, 2013 at 7:01 pm #141591andymacleod
ParticipantNearly there…
July 4, 2013 at 7:02 pm #141592andymacleod
Participant–
July 4, 2013 at 7:02 pm #141597andymacleod
Participant–
July 7, 2013 at 3:48 pm #141812andymacleod
ParticipantGot it working! Quite a struggle – this was a bit out of my depth (when I started it). Here’s my code… I really hope it helps someone else!
$taxonomy_name = ‘topic’;
$term = $wp_query->get_queried_object();
$term_id = $term->term_id;
$term_slug = $term->slug;
$args_cats = array(
‘taxonomy’ => $taxonomy_name,
‘hierarchical’ => true,
‘child_of’ => $term_id,
‘depth’ => ‘1’,
‘hide_empty’ => ‘0’,
‘title_li’ => ”,
‘echo’ => ‘0’,
);
$args_children = array(
‘parent’ => $term_id,
‘hide_empty’ => false
);
$children = get_terms( ‘topic’, $args_children);
if($children) {
echo wp_list_categories($args_cats);
} else {
$args = array(
‘post_type’ => ‘lessons’,
‘order’ => ‘ASC’,
‘tax_query’ => array (
array (
‘taxonomy’ => ‘topic’,
‘field’ => ‘slug’,
‘terms’ => $term_slug
)
)
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>- “/>
}
} else {
echo ‘No lessons found’;
}
wp_reset_postdata();
}
?> -
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.