Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Use wp_list_categories to navigate through custom taxonomy to custom post type

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #46106
    andymacleod
    Participant

    I’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

    #141545
    andymacleod
    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);
    }

    ?>

    #141591
    andymacleod
    Participant

    Nearly there…

    #141592
    andymacleod
    Participant

    #141597
    andymacleod
    Participant

    #141812
    andymacleod
    Participant

    Got 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();
    }
    ?>

Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘Back End’ is closed to new topics and replies.