0

I am working on a theme that has a custom post type called Languages and it will have a custom taxonomy called Language which will act as a category.

Now let's assume my Language taxonomy has these 4 values for now, English, French, German, Spanish.

I would like to have something like this with this custom taxonomy/category on my sidebar...

English
- link to newest post
- link to 2nd newest post
- link to 3rd newest post
- link to 4th newest post
- All the way to the 10th post
- then a link to View all

French
- link to newest post
- link to 2nd newest post
- link to 3rd newest post
- link to 4th newest post - All the way to the 10th post
- then a link to View all

German
- link to newest post
- link to 2nd newest post
- link to 3rd newest post
- link to 4th newest post - All the way to the 10th post
- then a link to View all

Spanish
- link to newest post
- link to 2nd newest post
- link to 3rd newest post
- link to 4th newest post - All the way to the 10th post
- then a link to View all

So as you can see, I just need to list all of a certain taxonomy, then under each one show the 10 newest post under that category ande provide a link to the view all page

Please help me do this someone

I know how to set up the taxonomy and custom post type, I just need help getting it to show each one and the 10 newest post for each one in a list like that

1 Answer 1

1

This code runs a foreach loop through each value associated with the language taxonomy then runs a query on that value to get the posts associated with it.

$taxonomies = get_categories( array( 'taxonomy' => 'language' ) );

    foreach ( $taxonomies as $tax ) {
        global $post;
        $args = array(
          'language' => $tax->category_nicename,
          'posts_per_page' => 10
          );

          $lang_query = new WP_Query( $args );

          echo $tax->name;
          echo '<ul>';
          while ($lang_query->have_posts() ) : $lang_query->the_post();

            echo '<li><a href="'.the_permalink().' ">' .the_title(). '</a> </li>';

            endwhile; wp_reset_postdata();

            echo '</ul>';
            echo '<a href=" '.get_category_link( $tax->term_id ).'">View all in ' .$tax->name. '</a>';

    }
2
  • Awesome thank you, do you think it is a bad idea to hit the DB like that on every page just to build a menu like this?
    – JasonDavis
    Commented Dec 8, 2011 at 6:19
  • WP_Query has some built in caching. I've only had problems with very large result sets 40K + posts. The other option is to use get_posts with a foreach loop.
    – Chris_O
    Commented Dec 8, 2011 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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