3

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>";
?>

1 Answer 1

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++;
    }

}
5
  • 1
    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, 2012 at 12:33
  • while is missing the closing ). I'll correct it above Oct 22, 2012 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, 2012 at 13:00
  • i changed the markup here, it had a small problem. What you mean by "failed output from cpt_auto"? Oct 22, 2012 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, 2012 at 13:52

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.