$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>custom post types - Add an A-Z menu at the top of A-Z listing code|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

custom post types - Add an A-Z menu at the top of A-Z listing code

matteradmin9PV0评论

I have the following code that is creating a and A-Z listing of my Posts in a Custom post type. I'd like to somehow create a A-Z menu at the top which links down to its respective anchor. I just can't seem to get this part to work. Any help much appreciated?

    <?php
   $args = array(
    'post_type' => bha_issues, 
    //'post_status' => 'bha_issues',
     'orderby' => 'title',
     'order' => 'ASC',
     'caller_get_posts' => 1,
     'posts_per_page' => 40,
                 );
query_posts($args);





if (have_posts()) {
    $curr_letter = '';
    while (have_posts()) {
        the_post();
        $this_letter = strtoupper(substr($post->post_title,0,1));

        if ($this_letter != $curr_letter) {
          echo "<div id='$this_letter' class='hidden'><a name='$this_letter'></a><h2>$this_letter </h2></div>";
          $curr_letter = $this_letter;
        }


    ?>

        <div id='$this_letter' class='hidden'><a href='<?php the_permalink() ?>'><?php the_title(); ?></a></div>
    <?php 
    }
}
?>

I have the following code that is creating a and A-Z listing of my Posts in a Custom post type. I'd like to somehow create a A-Z menu at the top which links down to its respective anchor. I just can't seem to get this part to work. Any help much appreciated?

    <?php
   $args = array(
    'post_type' => bha_issues, 
    //'post_status' => 'bha_issues',
     'orderby' => 'title',
     'order' => 'ASC',
     'caller_get_posts' => 1,
     'posts_per_page' => 40,
                 );
query_posts($args);





if (have_posts()) {
    $curr_letter = '';
    while (have_posts()) {
        the_post();
        $this_letter = strtoupper(substr($post->post_title,0,1));

        if ($this_letter != $curr_letter) {
          echo "<div id='$this_letter' class='hidden'><a name='$this_letter'></a><h2>$this_letter </h2></div>";
          $curr_letter = $this_letter;
        }


    ?>

        <div id='$this_letter' class='hidden'><a href='<?php the_permalink() ?>'><?php the_title(); ?></a></div>
    <?php 
    }
}
?>
Share Improve this question edited May 7, 2013 at 10:34 fuxia 107k39 gold badges255 silver badges461 bronze badges asked May 7, 2013 at 10:26 Mark CursleyMark Cursley 212 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I've done something similar before, for a glossary page. My code is below. Hopefully you can extrapolate from this. Note that I';m using WP_Query not query_posts, as this is best practice.

<?php
    $args = array(
        'post_type' => 'my_glossary_term',
        'orderby' => 'title',
        'order' => 'ASC',
        'posts_per_page' => -1
    );

    $query = new WP_Query( $args );

    $dl = '';
    $glossary_letter = '';
    $active_letters = array( );

    while ( $query->have_posts() ) {
        $query->next_post();
        $term_letter = strtoupper( substr( $query->post->post_title, 0, 1 ) );
        if ( $glossary_letter != $term_letter ) {
            $dl .= (count( $active_letters ) ? '</dl>' : '') . '<li id="' . $term_letter . '"><span class="subheading">' . $term_letter . '</span><dl>';
            $glossary_letter = $term_letter;
            $active_letters[] = $term_letter;
        }
        $dl .= '<dt>' . $query->post->post_title . '</dt>';
        $dl .= '<dd>' . $query->post->post_content . '</dd>';
    }
    $dl .= '</dl></li>';

    $ul = '<ul class="letters">';
    foreach ( array( 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' ) as $letter ) {
        $ul .= '<li>' . (in_array( $letter, $active_letters ) ? '<a href="#' . $letter . '">' . $letter . '</a>' : $letter) . '</li>';
    }
    $ul .= '</ul>';

    echo '<div id="glossary">' . $ul . '<ul class="definitions">' . $dl . '</ul></div>';
    ?>
Post a comment

comment list (0)

  1. No comments so far