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 make a glossary. It should looks like:

Index-Nav
0-9 | A B C ... M ... X Y Z

B BMW

M
Mercedes
...

ALL entries from index-nav (A-Z, O-9) should be visible in a <ul> and should linked to the content anchor (only if content isn´t empty). Entry without content should get an additional CSS class.

My code (thanks to @Mridul Aggarwal) shows just entries in index-nav when there is content, too.

Sorry, I´m not an english native speaker.

page-my-custom-post-type.php

<?php

$index = 0;
$terms = get_terms('marke');
$range = array_merge(range(0, 9), range('A', 'Z'));

echo '<ul>';
foreach ($terms as $term) {

    if(ord($range[$index]) <= ord(strtoupper(substr($term->name, 0, 1)))) {
        while($range[$index] != strtoupper(substr($term->name, 0, 1))) {
            echo '<li>'. $range[$index] . '</li>';
            $index++;
        }
        $index = strtoupper(substr($term->name, 0, 1));
        echo "<li><a href='" . "#" . glossar_ . $index . "' />$index</a></li>";
        $index++;
    }

}
echo '</ul>';
?>

    <h2><?php echo $term->name; ?></h2>
    <?php $args = array( 'post_type' => 'cpt_auto', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', 
        'tax_query' => array(
            array(
                'taxonomy' => 'marke',
                '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;

 echo "<a href='" . "#" . "' />topFooter</a>";
?>
share|improve this question

1 Answer 1

$index = 0;
$terms = get_terms('marke');
$range = array_merge(range(0, 9), range('A', 'Z'));

echo '<ul>';
foreach ($terms as $term) {

    if(ord($range[$index]) <= ord(strtoupper(substr($term->name, 0, 1)))) {
        while($range[$index] != strtoupper(substr($term->name, 0, 1))) {
            echo '<li>'. $range[$index] . '</li>';
            $index++;
        }
        $index = strtoupper(substr($term->name, 0, 1));
        echo "<li><a href='".get_term_link($term)."' />{$range[$index]}</a></li>";
        $index++;
    }

}
share|improve this answer
    
Thanks, again. I became for this line: while($range[$index] != strtoupper(substr($term->name, 0, 1)) { this message: Parse error: syntax error, unexpected '{' –  ogni Oct 22 '12 at 12:33
    
while is missing the closing ). I'll correct it above –  Mridul Aggarwal Oct 22 '12 at 12:36
    
I chance my code above, too. Now failed output from the cpt_auto. It will just show a "B"? –  ogni Oct 22 '12 at 13:00
    
i changed the markup here, it had a small problem. What you mean by "failed output from cpt_auto"? –  Mridul Aggarwal Oct 22 '12 at 13:11
    
I mean that nothing will be show on the page without "B" and the page will not stop to load. –  ogni Oct 22 '12 at 13:52

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.