WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Archive pages showing custom taxonomies (not the post in that taxonomy) (3 posts)

  1. Ruud Evers
    Member
    Posted 2 years ago #

    Hi there,

    I have created a custom post type and a custom taxonomy type with two levels of taxonomies. Let me make it clear with the following example:

    Custom post type: Movie
    Custom taxonomy type: Genres
    Custom taxonomy lists:
    - Comedy
    - Comedy sub genre 1
    - Comedy sub genre 2
    - Action
    - Action sub genre 1
    - Action sub genre 2
    - Sci-Fi
    - Sci-Fi sub genre 1
    - Sci-Fi sub genre 2

    What I want to achieve is the following:
    1) When going to the Genre-archive, it should display the main level taxonomies (Comedy, Action, Sci-Fi);
    2) When going to the Comedy-archive, it should display the sub genres;
    3) When going to the sub genre-archive, it should display all movies in that custom genre.

    I have achieved step 3 by using the following code:

    <?php
    	$term = $wp_query->queried_object; get_term( $term->slug, 'genres', '', '' ) ;
    	query_posts( array( 'post_type' => 'movie', 'genres' => $term->slug ) );
    	if ( have_posts() ) : while ( have_posts() ) : the_post();
    ?>
    		<h3><?php the_title(); ?></h3>
    		<?php the_content(); ?>
    <?php endwhile; endif; wp_reset_query(); ?>

    Using the code below, I am able to display a full tree view of all my taxonomies:

    <?php
    	//list terms in a given taxonomy using wp_list_categories (also useful as a widget if using a PHP Code plugin)
    
    	$taxonomy     = 'genres';
    	$orderby      = 'name';
    	$show_count   = 0;      // 1 for yes, 0 for no
    	$pad_counts   = 0;      // 1 for yes, 0 for no
    	$hierarchical = 1;      // 1 for yes, 0 for no
    	$hide_empty   = 0;
    	$title        = '';
    	$parent       = '';
    
    	$args = array(
    	  'taxonomy'     => $taxonomy,
    	  'orderby'      => $orderby,
    	  'show_count'   => $show_count,
    	  'pad_counts'   => $pad_counts,
    	  'hierarchical' => $hierarchical,
    	  'child_of'     => $parent,
    	  'hide_empty'   => $hide_empty,
    	  'title_li'     => $title
    	);
    ?>
    	<ul>
    		<?php wp_list_categories( $args ); ?>
    	</ul>

    What I am thus looking for is an archive template file that does the following:
    1) Check if it is a main level taxonomy. Yes => show a list of the sub taxonomies
    2) Check if it is a sub-taxonomy. Yes => show a list of the posts in this sub-taxonomy.

    Anyone know if this is possible?

    Thanks a lot!

  2. Ruud Evers
    Member
    Posted 2 years ago #

    I am progressing a little here. Using the following code, I am able to list all the sub taxonomies using the parents taxonomy id:

    <?php
    	$termID = 12;
    	$taxonomyName = "act_groups";
    	$termchildren = get_term_children( $termID, $taxonomyName );
    
    	echo '<ul>';
    	foreach ($termchildren as $child) {
    		$term = get_term_by( 'id', $child, $taxonomyName );
    		echo '<li><a href="' . get_term_link( $term->name, $taxonomyName ) . '">' . $term->name . '</a></li>';
    	}
    	echo '</ul>';
    	?>

    However, so far I haven't been able yet to find a way to get the termID from the database, thus making the function dynamic.

    Any help would be appreciated.

  3. Ruud Evers
    Member
    Posted 2 years ago #

    This page put me on the right path: http://www.wpbeginner.com/wp-tutorials/how-to-display-child-taxonomy-on-parent-taxonomys-archive-page

    I managed what I wanted by using the following code:

    <?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
    if ($term->parent == 0) { ?>
    	<?php
    		wp_list_categories('taxonomy=YOUR-TAXONOMY-NAME&depth=1&show_count=0&hide_empty=0&title_li=&child_of=' . $term->term_id);
    	?>
    <?php } else { ?>
    	<?php
    		$term = $wp_query->queried_object; get_term( $term->slug, 'YOUR-TAXONOMY-NAME', '', '' ) ;
    		query_posts( array( 'post_type' => 'YOU-CUSTOM-POST-TYPE', 'YOUR-TAXONOMY-NAME' => $term->slug ) );
    		if ( have_posts() ) : while ( have_posts() ) : the_post();
    	?>
    		<h3><?php the_title(); ?></h3>
    		<?php the_content(); ?>
    	<?php endwhile; endif; wp_reset_query(); ?>
    <?php } ?>

Topic Closed

This topic has been closed to new replies.

About this Topic