Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Hello, I have been stumped for a few days on a perfect way to display the posts using my custom post type (track) and the taxonomies ( genre, sub_genre). When a user goes to http://sitename.com/genre/"sub_genre name" , i want all posts of that sub_genre to be categorized by those sub_genre taxonomies with content. ex..

http://sitename.com/genre/hip_hop

Genre: West

junior test track

Genre: Old School

Song 3 track

Rap title track

Song 2 track

This is the code Im using but but its looping all wrong...

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post();
$cam_brands = get_the_terms( $post->ID, 'sub_genre' );?>
<?php foreach( $cam_brands as $brand ) : ?>
<h3 class="genre-title"> <?php echo $brand->name; ?> </h3>

<?php $wpq = array( 'post_type' => 'track', 'taxonomy' => 'sub_genre', 'term' => $brand->slug, 'post_status' => 'publish','posts_per_page' => -1, 'caller_get_posts'=> 1 );
$brand_posts = new WP_Query ($wpq);?>
<ul>
<?php foreach( $brand_posts->posts as $post ) : ?>
<li> <?php echo $post->post_title; ?> <a class="sc-player" href="<?php echo get('soundcloud_link'); ?>">track</a> </li>
<?php endforeach ?>
</ul>
<?php endforeach ?>
<?php endwhile; ?>
<?php endif; ?>

Its seems to be going through the foreach as many times as there are actual posts in the genre taxonomy rather than the sub_genre.

ex.

Genre:West

junior test track

Genre:Old School

Song 3 track

Rap title track

Song 2 track

Genre:Old School

Song 3 track

Rap title track

Song 2 track

Genre:Old School

Song 3 track

Rap title track

Song 2 track

Any help Please Please Please would be appreciated. My php skills are not the best and the loop is making my brain hurt a little. Thanks

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

What you are doing

Your foreach is nested inside while (have_posts()) : the_post();. This while loop, loops through each of your posts (in the genre 'hip_hop').

So for each post in the genre 'hip_hop', you find the post's taxonomy terms for the taxonomy 'sub_genre' (this is the contents of $cam_brands). Then, for each associated term (in $cam_brands) you get all posts associated to that term (or $brand).

You final foreach loops through each of these posts and displays them.

What I think you are trying to do

I'm not aware of any native and straightforward way WordPress can 'split' all the posts of a particular taxonomy term into it's children. But the following will do that (albeit, not in a particularly efficient way).

Also, it appears 'sub_genre' is a separate taxonomy from 'genre' - am I right? If you want a parent-child relationship, you should simply make 'genre' a hierarchal taxonomy, and create sub-genres in the same way that you create 'sub-categories'. Once you've done that, the following should work with the below caveats.

$term = get_queried_object();

//Get all children of this term;
$children = get_terms( 'genre', 
    array(
        'parent'=>$term->term_id;
    ) ) ;

if($children):
    //We have an array of child (of current genre) terms. Loop through and display the posts
    foreach($children as $child):
        echo '<h2>'.$child->slug.'</h2>';

        // The Query - get all posts for this child-genre.
        $args = array('post_type' => 'track', 'taxonomy' => 'genre', 'term' => $child->slug, 'post_status' => 'publish','posts_per_page' => -1);
        $child_posts = new WP_Query ($args);

        // The Loop - display all posts for this child-genre.
        while ( $child_posts->have_posts() ) : $child_posts->the_post();
            echo '<li>';
                the_title();
            echo '</li>';
        endwhile;

        // Reset Post Data
        wp_reset_postdata();
    endforeach;
else:
    //On genre with no children - you may want to do something else.
    echo 'No sub-genre for this genre found';
endif;

Caveats

  • This code isn't tested :D (but should work)
  • It relies on your genre/sub-genres all being part of one hierarchal
  • Visiting a genre with no children will display the 'no sub-genre' message.
  • Tracks which have are associated with a genre term, but not any of its children will not be displayed.
share|improve this answer
    
Thanks Stephen. I cant thank you enough!!! This is perfect. I only had to edit a few lines to make it work. foreach( $children as $children ) : and ` while ( $child_posts->have_posts() ) : $child_posts->the_post(); ` –  Khey Feb 7 '12 at 17:54
    
@Khey glad it worked, I've made the corrections. –  Stephen Harris Feb 7 '12 at 18:14
    
Any thoughts on how I could paginate the genres? –  Khey Feb 22 '12 at 16:55
    
Independently? Not straightforward. WP uses the variable paged to handle pagination, you would need to register a separate paged variable for each genre, e.g. OldSchoolPaged, WestPaged to store the individual 'pages' you are on –  Stephen Harris Feb 22 '12 at 17:03
    
Interesting. I'm not quite sure what you mean. Are you saying I would need to layout the taxonomies in a different variation then having parent and children taxonomies? Is it possible to replace the foreach($children as $child): with a query that has posts_per_page => 2? –  Khey Feb 22 '12 at 17:30
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.