0

So I have a custom post type called Video and a taxonomy called category that I use to group videos togheter, so I want to list them like this:

Taxonomy NAME 1
VIDEO 1
VODEO 2

Taxonomy NAME 2
Video 1
Video 2

So I tried something but it doesn't work, the problem is that for each video I also display the taxonomy name like this:

Taxonomy Name 1
Video 1
Taxonomy Name 1
Video 2

So this is not what I want, mabe you guys can figure it out, what I do wrong:

<?php get_header();?>
<?php
    $args = array( 'post_type' => 'Video');
    $loop = new WP_Query( $args );
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
        <?php $fg = get_the_terms(get_the_ID(),'category'); ?>
        <?php foreach($fg as $term): ?>
                <h2><?php echo $term->name; ?></h2>
                <?php wp_reset_postdata(); ?>
                <?php
                        $posts = new WP_Query(array('post_type' => 'Video', 'category' => $term->name) );
                        while ( $posts->have_posts() ) : $posts->the_post();
                            the_post_thumbnail();
                            ?>
                            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                            <?php
                        endwhile;
                ?>
        <?php endforeach; ?>
<?php endwhile; ?>

<?php get_footer(); ?>

If someone can help please do it :( I can't get this

2
  • I hope so... :( 10x
    – Uffo
    Jan 12 '12 at 23:47
  • Store the current taxonomy name in a variable, during every iteration compare the new name and the old name, if different, echo the taxonomy name.
    – Anand
    Jan 13 '12 at 12:21
1

As per Anand's comment, something like this should work.

<?php get_header();?>

<?php
    $args = array( 'post_type' => 'Video');
    $loop = new WP_Query( $args );

    $old_term = null;
    while ( $loop->have_posts() ):
        $loop->the_post();
        $fg = get_the_terms(get_the_ID(),'category');
        foreach($fg as $term):
            if ( $old_term === null || $old_term != $term ):
                $old_term = $term;
?>
                <h2><?php echo $term->name; ?></h2>
<?php
            endif;

            $posts = new WP_Query(array('post_type' => 'Video', 'category' => $term->name) );
            while ( $posts->have_posts() ) : $posts->the_post();
                the_post_thumbnail();
?>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php
            endwhile;

            wp_reset_postdata();
        endforeach;
    endwhile;
?>

<?php get_footer(); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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