I have 6 custom taxonomies setup on my site, registered in the following way (Taxonomy in the example is named 'Topic'):
/* Topic */
add_action( 'init', 'register_taxonomy_topic' );
function register_taxonomy_topic() {
$labels = array(
'name' => _x( 'Topics', 'topic' ),
'singular_name' => _x( 'Topic', 'topic' ),
'search_items' => _x( 'Search Topics', 'topic' ),
'popular_items' => _x( 'Popular Topics', 'topic' ),
'all_items' => _x( 'All Topics', 'topic' ),
'parent_item' => _x( 'Parent Topic', 'topic' ),
'parent_item_colon' => _x( 'Parent Topic:', 'topic' ),
'edit_item' => _x( 'Edit Topic', 'topic' ),
'update_item' => _x( 'Update Topic', 'topic' ),
'add_new_item' => _x( 'Add New Topic', 'topic' ),
'new_item_name' => _x( 'New Topic Name', 'topic' ),
'separate_items_with_commas' => _x( 'Separate topics with commas', 'topic' ),
'add_or_remove_items' => _x( 'Add or remove topics', 'topic' ),
'choose_from_most_used' => _x( 'Choose from the most used topics', 'topic' ),
'menu_name' => _x( 'Topics', 'topic' ),
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_in_nav_menus' => true,
'show_ui' => true,
'show_tagcloud' => true,
'hierarchical' => true,
'rewrite' => true,
'query_var' => true
);
register_taxonomy( 'topic', array('post'), $args );
}
Within these 6 taxonomies, there are between 2 to 25 different terms.
I am trying to create a dynamic archive page that will list the correct posts associated with a specific term, when a user clicks through to the archive page of that term. The page needs to be dynamic, i.e list the relevant posts based on the selected term, using one template, rather than creating a template file for each term (which would result in well over 100 templates!)
Below is an example of the structure of my taxonomies:
Post type: Post, Taxonomy: Topic, Term(s): One, Two, Three
The loop I am currently working with is as follows, which seems to output a list of articles, but I cannot decipher from where these posts are coming from, and many of them are duplicated! My loop also specifies 'topic', where as I need it to dynamically generate the content regardless of which of the 6 taxonomies and X number of terms is being viewed.
<?php //start by fetching the terms for the animal_cat taxonomy
$terms = get_terms( 'topic', array(
'orderby' => 'count',
'hide_empty' => 0
) );
?>
<?php
// now run a query for each animal family
foreach( $terms as $term ) {
// Define the query
$args = array(
'post_type' => 'post',
'topic' => $term->slug
);
$query = new WP_Query( $args );
// output the post titles in a list
echo '<ul>';
// Start the Loop
while ( $query->have_posts() ) : $query->the_post(); ?>
<li id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile;
echo '</ul>';
// use reset postdata to restore orginal query
wp_reset_postdata();
} ?>
Any help greatly appreciated!