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

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

}

Leave a Reply

Your email address will not be published. Required fields are marked *