Community Forums › Forums › Archived Forums › General Discussion › List of post by taxonomy
Tagged: Custom post, loop, taxonomy
- This topic has 5 replies, 3 voices, and was last updated 9 years, 9 months ago by
rgval.
-
AuthorPosts
-
June 11, 2015 at 4:29 pm #155906
rgval
MemberHi, I am trying to make a list of students organized by graduation year.
I created a custom post type called 'student' with a taxonomy 'graduation-year'.I have replaced the genesis loop to create my own listing, but somehow it only shows the posts in the first year, the other years are listed but without posts.
This is the code for my WP archive template: archive-student.php
(mostly based on http://code.tutsplus.com/tutorials/taxonomy-archives-list-posts-by-taxonomys-terms--cms-20045)<?php /** * STUDENT ARCHIVE * * @author RGV */ // Replace the genesis loop remove_action( 'genesis_loop', 'genesis_do_loop' ); add_action( 'genesis_loop', 'my_custom_loop' ); function my_custom_loop() { //start by fetching the terms for the year taxonomy $terms = get_terms( 'graduation-year', array( 'orderby' => 'name', 'order' => 'DESC' ) ); // now run a query of students for each year foreach( $terms as $term ) { // Define the query $args = array( 'post_type' => 'student', 'year' => $term->slug ); $query = new WP_Query( $args ); // output the term name in a heading tag echo'<h2>' . $term->name . '</h2>'; // output the post titles in a list echo '<ul>'; // Start the Loop while ( $query->have_posts() ) : $query->the_post(); ?> <li class="student-list" id="post-<?php the_ID(); ?>"> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </li> <?php endwhile; echo '</ul>'; // use reset postdata to restore original query wp_reset_postdata(); } } genesis(); ?>
Any idea what I am doing wrong? Is there a better way of getting this kind of results using genesis?
Thanks a lot!
June 12, 2015 at 6:56 am #155974Brad Dalton
ParticipantJune 12, 2015 at 9:06 am #155980PatrickODacre
MemberThis may be obvious, but are you sure you've applied posts to the other years? 🙂
June 12, 2015 at 12:45 pm #155994rgval
Member😀 Yes, good point. I do have post with every taxonomy.
I confirmed they work with the taxonomy permalinkJune 12, 2015 at 12:48 pm #155995rgval
MemberThanks braddalton. What I am trying to do is a template for the custom post archive, so I can list all posts in one page.
I am surprised is it not more easy to list posts by years...(not the publication year, but a custom numeric field). Apart from my problems with the loop, I though this should a be quite common way of sorting content. I am doing something wrong? perhaps another more efficient structure in the posts, etc.
June 12, 2015 at 1:54 pm #156010rgval
MemberI am so sorry, guys, it was a mistake in my taxonomy name.
I used 'year' as the taxonomy term, and is reserved for WP.I am still surprised about how difficult is to list posts grouped by a custom taxonomy in an archive.
I thought it could be easier with Genesis (perhaps I'm doing something wrong and there is an easier way...)Anyway, the working code is here:
<?php /** * STUDENT ARCHIVE */ // Replace the genesis loop remove_action( 'genesis_loop', 'genesis_do_loop' ); add_action( 'genesis_loop', 'my_custom_loop' ); function my_custom_loop() { //start by fetching all terms for the year taxonomy $terms = get_terms( 'graduation-year', array( 'orderby' => 'name', 'order' => 'DESC' ) ); // now run a query of students for each year foreach( $terms as $term ) { // Define the query $args = array( 'post_type' => 'student', 'graduation-year' => $term->slug ); $query = new WP_Query( $args ); // output the term name in a heading tag echo'<h2>' . $term->name . '</h2>'; // output the post titles in a list echo '<ul>'; // Start the Loop while ( $query->have_posts() ) : $query->the_post(); ?> <li class="student-list" 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(); } } genesis(); ?>
-
AuthorPosts
- The topic ‘List of post by taxonomy’ is closed to new replies.