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.

I´d like to display all car manufacturer in order by title (e.g. "Merecedes") and order from lowest to highest values (e.g. "A-Class") "ASC", too. It should looks like a glossary.

Page: Cars

A ...

BMW:
1er
2er
3er
...

Mercedes:
A-Class
B-Class
C-Class
...

Z

This is my custom-port-type-cars.php

<h2>Mercedes</h2>
    <?php $args = array( 'post_type' => 'cars', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', 

    // ============================= Mercedes ==============================
        'tax_query' => array(
            array(
                'taxonomy' => 'manufacturer',
                'field' => 'slug',
                'terms' => array('mercedes')
            )
        )
    );

    // ============================= OUTPUT ==============================
    $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post();
        the_title('<h3>', '</h3>');
        the_content();
    endwhile; ?>


<h2>BMW</h2> 
    <?php $args = array( 'post_type' => 'cars', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', 

    // ============================= BMW ==============================
        'tax_query' => array(
            array(
                'taxonomy' => 'manufacturer',
                'field' => 'slug',
                'terms' => array('bmw')
            )
        )
    );


    // ============================= OUTPUT ==============================
    $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post();
        the_title('<h3>', '</h3>');
        the_content();
    endwhile; ?>

This doesn´t work for me. I can´t order title an terms at the same time.

Thanks for your help
Ogni

share|improve this question
    
You forgot the question. –  toscho Oct 21 '12 at 10:02
    
So you are ordering manually by terms and with WP_Query by titles. Where does it go wrong? Any example of output? –  Miha Rekar Oct 21 '12 at 10:16

1 Answer 1

up vote 0 down vote accepted
$index = 'A';
$terms = get_terms('manufacturer');

foreach ($terms as $term) {
    if($index != strtoupper(substr($term->name, 0, 1))) {
        $index = strtoupper(substr($term->name, 0, 1));
        echo '<h1>'. $index . '</h1>';
    }
    ?>
    <h2><?php echo $term->name; ?></h2>
    <?php $args = array( 'post_type' => 'cars', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', 
        'tax_query' => array(
            array(
                'taxonomy' => 'manufacturer',
                'field' => 'slug',
                'terms' => array($term->slug)
            )
        )
    );

    // ============================= OUTPUT ==============================
    $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post();
        the_title('<h3>', '</h3>');
        the_content();
    endwhile;
}
share|improve this answer
    
Thanks for your help. Can I add a glossary index-nav like "A", "B BMW", "C", ... "M Mercedes", too? –  ogni Oct 21 '12 at 11:28
    
edited the answer –  Mridul Aggarwal Oct 21 '12 at 11:58
    
Perfect for me - great and thank you! –  ogni Oct 21 '12 at 21:44

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.