1

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

2
  • You forgot the question.
    – fuxia
    Commented Oct 21, 2012 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
    Commented Oct 21, 2012 at 10:16

1 Answer 1

0
$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;
}
2
  • Thanks for your help. Can I add a glossary index-nav like "A", "B BMW", "C", ... "M Mercedes", too?
    – ogni
    Commented Oct 21, 2012 at 11:28
  • edited the answer Commented Oct 21, 2012 at 11:58

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.