Community Forums › Forums › Archived Forums › General Discussion › Template listing all posts in entire custom taxonomy
Tagged: custom taxonomy
- This topic has 3 replies, 2 voices, and was last updated 10 years, 9 months ago by
Brad Dalton.
-
AuthorPosts
-
June 19, 2014 at 1:03 pm #110599
carrieoke13
ParticipantHi,
I've created a custom taxonomy on my site called "schools" for tagging regular posts with school information. Now I'm wanting to have a page that shows all posts in the "schools" taxonomy, organized by their terms. So, I have two terms in the "schools" taxonomy thus far - Parsons and UGA. I want the page to show:
Parsons
Post 1
Post 2UGA
Post 1
Post 2I've gotten as far as a listing of the terms in the taxonomy by creating a custom page template using this:
<?php /** * Template Name: School Listing */ add_action( 'genesis_entry_content', 'add_taxonomy_list', 5 ); function add_taxonomy_list() { $args = array( 'format' => 'list', 'taxonomy' => 'schools', ); wp_tag_cloud( $args); } genesis();
and it's working (unstyled) here: http://identitywebandphoto.com/abby/wordpress/lesson-listing/
How would I show the posts under each of those terms?
Thanks,
CarrieJune 19, 2014 at 5:32 pm #110680Brad Dalton
ParticipantCreate a new WP_Query http://codex.wordpress.org/Class_Reference/WP_Query
You could then use get_template_part() to output the lists in any WordPress or Genesis hook position.
Another option is to create 2 new template files and use the WordPress Template Hierarchy. One for each term and then use get_template_part() to display them in any hook position or directly in any template.
June 19, 2014 at 8:21 pm #110707carrieoke13
ParticipantHi Brad,
so I've got something working but it's a page template and I feel like maybe there is a better way to do it using Genesis hooks?I used the Minimum Pro archive portfolio template and put this in it (because I want the list styled like the portfolio template):
//this loop returns all posts separated by genres they belong to $post_type = 'post'; // <-- put your custom post type here $tax = 'schools'; // <-- put your custom taxonomy here $tax_terms = get_terms($tax); if ($tax_terms) { foreach ($tax_terms as $tax_term) { $args=array( 'post_type' => $post_type, "$tax" => $tax_term->slug, 'post_status' => 'publish', 'posts_per_page' => -1, 'caller_get_posts'=> 1 ); $my_query = null; $my_query = new WP_Query($args); if( $my_query->have_posts() ) { echo $tax_term->name; while ($my_query->have_posts()) : $my_query->the_post(); ?> <div class="portfolio-image"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></div> <?php if ( $image = genesis_get_image( 'format=url&size=portfolio' ) ) { printf( '<div class="portfolio-image"><a href="%s" rel="bookmark"><img src="%s" alt="%s" /></a></div>', get_permalink(), $image, the_title_attribute( 'echo=0' ) ); } endwhile; } wp_reset_query(); } } }
and here's what it looks like right now: http://identitywebandphoto.com/abby/wordpress/testing-posts/
June 19, 2014 at 10:46 pm #110726Brad Dalton
ParticipantThat's another way to do it however i think a custom genesis loop is best to maintain styling.
You could also use http://codex.wordpress.org/Function_Reference/wp_get_post_terms
You could output using a custom function with hook and template tag like this:
Change comments to the name of your file and template using get_template_part()
-
AuthorPosts
- The topic ‘Template listing all posts in entire custom taxonomy’ is closed to new replies.