0

I created a site for my company recently and previously, I was simply manually changing a variable and had the same code copied and pasted 26 times one after the other, but I figured I should loopify it to make it a little bit easier to debug or make changes to.

Here's code with the loop.

<div class="col_half">
<div class="alphabet-sort">
    <ul class="alphabet-list" style="padding:0px;margin-left:0px;">
        <li class="alphabet-sort-item first"><a href="#letter-A">A</a></li>
        <li class="alphabet-sort-item"><a href="#letter-B">B</a></li>
        <li class="alphabet-sort-item"><a href="#letter-C">C</a></li>
        <li class="alphabet-sort-item"><a href="#letter-D">D</a></li>
        <li class="alphabet-sort-item"><a href="#letter-E">E</a></li>
        <li class="alphabet-sort-item"><a href="#letter-F">F</a></li>
        <li class="alphabet-sort-item"><a href="#letter-G">G</a></li>
        <li class="alphabet-sort-item"><a href="#letter-H">H</a></li>
        <li class="alphabet-sort-item"><a href="#letter-I">I</a></li>
        <li class="alphabet-sort-item last"><a href="#letter-J">J</a></li>
      </ul>
</div>
<style>
span.category-inactive-member {
  display:none !important;
}
.listed-member {
  line-height:1;
}
</style>
<?php
    //get all post IDs for posts beginning with cap B, in title order,
        //display posts
$categories = get_the_category();
$alpha_char = 1;

while ($alpha_char <= 10) {
  if ($alpha_char == 1) {
  $first_char = 'A';
}
else if ($alpha_char == 2) {
  $first_char = 'B';
}
else if ($alpha_char == 3) {
  $first_char = 'C';
}
else if ($alpha_char == 4) {
  $first_char = 'D';
}
else if ($alpha_char == 5) {
  $first_char = 'E';
}
else if ($alpha_char == 6) {
  $first_char = 'F';
}
else if ($alpha_char == 7) {
  $first_char = 'G';
}
else if ($alpha_char == 8) {
  $first_char = 'H';
}
else if ($alpha_char == 9) {
  $first_char = 'I';
}
else if ($alpha_char == 10) {
  $first_char = 'J';
}
  echo $first_char;
  echo $alpha_char;
        echo '<table class="alphabet" style="border:0px;border-color:transparent;">
                    <tbody><tr>
                        <td style="border-color:transparent;padding-left:5px;padding-right:5px;"><span id="letter-' . $first_char . '"><strong>' . $first_char . '</strong></span></td>
                        <td width="100%" style="border-color:transparent;"><span class="separator-line"></span></td>
                    </tr>
                </tbody></table>';
        $postids=$wpdb->get_col($wpdb->prepare("
        SELECT      ID
        FROM        $wpdb->posts
        WHERE       SUBSTR($wpdb->posts.post_title,1,1) = %s
        ORDER BY    $wpdb->posts.post_title",$first_char)); 

        if ($postids) {
        $args=array(
          'post__in' => $postids,
          'post_type' => 'page',
          'post_status' => 'publish',
          'posts_per_page' => -1,
          'caller_get_posts'=> 1.,
          'orderby' => 'title',
          'order' => 'ASC',
        );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts()) {
            while ($my_query->have_posts()) : $my_query->the_post();
            $current_meta_value = get_post_meta(get_the_ID(), 'featured-checkbox', true);
            $inactive_meta_value = get_post_meta(get_the_ID(), 'inactive-member', true);
            $backup_phone_meta_value = get_post_meta(get_the_ID(), 'backup-answering', true);
            $backup_phone_meta_value = get_post_meta(get_the_ID(), 'backup-answering', true);
            if ( $inactive_meta_value != 'yes' ) { ?>
            <p class="listed-member notmem<?php echo $current_meta_value; ?>"><span <?php post_class(); ?>><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?><?php $member_meta_value = get_post_meta(get_the_ID(), 'member-checkbox', true); $phone_meta_value = get_post_meta(get_the_ID(), 'phone-checkbox', true); $subtitle_meta = get_post_meta(get_the_ID(), 'subtitle', true); ?><?php if( $subtitle_meta != '' ) {echo ' (' . $subtitle_meta . ')';};?></a><img src="/wp-content/uploads/2016/09/member_icon.png" class="member-icon-small<?php echo $member_meta_value ?>"/><?php if($backup_phone_meta_value == 'no') { echo '<img src="/wp-content/themes/odextranet/images/icons/phone_icon.png" class="phone-icon-small' . $phone_meta_value . '" />';}else if($backup_phone_meta_value == 'yes') {echo '<img src="/wp-content/themes/odextranet/images/icons/phone_icon.png" class="phone-icon-small' . $backup_phone_meta_value . '" />';}?></span></span>
            </p>
            <?php }
          endwhile;
        }
        $alpha_char = $alpha_char + 1;
        echo $alpha_char;
        wp_reset_query();  // Restore global post data stomped by the_post().
        }
      }
    ?>
</div>
<div class="col_half col_last">
<div class="alphabet-sort">
      <ul class="alphabet-list" style="padding:0px;margin-left:0px;">
        <li class="alphabet-sort-item first"><a href="#letter-K">K</a></li>
        <li class="alphabet-sort-item"><a href="#letter-L">L</a></li>
        <li class="alphabet-sort-item"><a href="#letter-M">M</a></li>
        <li class="alphabet-sort-item"><a href="#letter-N">N</a></li>
        <li class="alphabet-sort-item"><a href="#letter-O">O</a></li>
        <li class="alphabet-sort-item"><a href="#letter-P">P</a></li>
        <li class="alphabet-sort-item"><a href="#letter-Q">Q</a></li>
        <li class="alphabet-sort-item"><a href="#letter-R">R</a></li>
        <li class="alphabet-sort-item"><a href="#letter-S">S</a></li>
        <li class="alphabet-sort-item"><a href="#letter-T">T</a></li>
        <li class="alphabet-sort-item"><a href="#letter-U">U</a></li>
        <li class="alphabet-sort-item"><a href="#letter-V">V</a></li>
        <li class="alphabet-sort-item"><a href="#letter-W">W</a></li>
        <li class="alphabet-sort-item"><a href="#letter-X">X</a></li>
        <li class="alphabet-sort-item"><a href="#letter-Y">Y</a></li>
        <li class="alphabet-sort-item last"><a href="#letter-Z">Z</a></li>
      </ul>
</div>
<?php
    $alpha_char = 1;

while ($alpha_char <= 10) {
  if ($alpha_char == 1) {
  $first_char = 'K';
}
else if ($alpha_char == 2) {
  $first_char = 'L';
}
else if ($alpha_char == 3) {
  $first_char = 'M';
}
else if ($alpha_char == 4) {
  $first_char = 'N';
}
else if ($alpha_char == 5) {
  $first_char = 'O';
}
else if ($alpha_char == 6) {
  $first_char = 'P';
}
else if ($alpha_char == 7) {
  $first_char = 'Q';
}
else if ($alpha_char == 8) {
  $first_char = 'R';
}
else if ($alpha_char == 9) {
  $first_char = 'S';
}
else if ($alpha_char == 10) {
  $first_char = 'T';
}
else if ($alpha_char == 11) {
  $first_char = 'U';
}
else if ($alpha_char == 12) {
  $first_char = 'V';
}
else if ($alpha_char == 13) {
  $first_char = 'W';
}
else if ($alpha_char == 14) {
  $first_char = 'X';
}
else if ($alpha_char == 15) {
  $first_char = 'Y';
}
else if ($alpha_char == 16) {
  $first_char = 'Z';
}
  echo $first_char;
  echo $alpha_char;
        echo '<table class="alphabet" style="border:0px;border-color:transparent;">
                    <tbody><tr>
                        <td style="border-color:transparent;padding-left:5px;padding-right:5px;"><span id="letter-' . $first_char . '"><strong>' . $first_char . '</strong></span></td>
                        <td width="100%" style="border-color:transparent;"><span class="separator-line"></span></td>
                    </tr>
                </tbody></table>';
        $postids=$wpdb->get_col($wpdb->prepare("
        SELECT      ID
        FROM        $wpdb->posts
        WHERE       SUBSTR($wpdb->posts.post_title,1,1) = %s
        ORDER BY    $wpdb->posts.post_title",$first_char)); 

        if ($postids) {
        $args=array(
          'post__in' => $postids,
          'post_type' => 'page',
          'post_status' => 'publish',
          'posts_per_page' => -1,
          'caller_get_posts'=> 1.,
          'orderby' => 'title',
          'order' => 'ASC',
        );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts()) {
            while ($my_query->have_posts()) : $my_query->the_post();
            $current_meta_value = get_post_meta(get_the_ID(), 'featured-checkbox', true);
            $inactive_meta_value = get_post_meta(get_the_ID(), 'inactive-member', true);
            $backup_phone_meta_value = get_post_meta(get_the_ID(), 'backup-answering', true);
            $backup_phone_meta_value = get_post_meta(get_the_ID(), 'backup-answering', true);
            if ( $inactive_meta_value != 'yes' ) { ?>
            <p class="listed-member notmem<?php echo $current_meta_value; ?>"><span <?php post_class(); ?>><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?><?php $member_meta_value = get_post_meta(get_the_ID(), 'member-checkbox', true); $phone_meta_value = get_post_meta(get_the_ID(), 'phone-checkbox', true); $subtitle_meta = get_post_meta(get_the_ID(), 'subtitle', true); ?><?php if( $subtitle_meta != '' ) {echo ' (' . $subtitle_meta . ')';};?></a><img src="/wp-content/uploads/2016/09/member_icon.png" class="member-icon-small<?php echo $member_meta_value ?>"/><?php if($backup_phone_meta_value == 'no') { echo '<img src="/wp-content/themes/odextranet/images/icons/phone_icon.png" class="phone-icon-small' . $phone_meta_value . '" />';}else if($backup_phone_meta_value == 'yes') {echo '<img src="/wp-content/themes/odextranet/images/icons/phone_icon.png" class="phone-icon-small' . $backup_phone_meta_value . '" />';}?></span></span>
            </p>
            <?php }
          endwhile;
        }
        $alpha_char = $alpha_char + 1;
        echo $alpha_char;
        wp_reset_query();  // Restore global post data stomped by the_post().
        }
      }
    ?>
</div>

Basically I have two vairables: $alpha_char and $first_char. The loop finds the numerical value for $alpha_char, then the if statements change it to an alphabetical letter. A bit unconventional, I know but it works. Aside from one issue. For some reason, when it reaches x, y, and z, it loops infinitely. I'v even tried separating them from the loop, to see if maybe it has some kind of limit, which it does not. For some reason if X, Y, or Z are set as the $first_char, there is an issue. I replaced them all with the letter N and had no issues whatsoever so it seems to just be something with those letters.

  • It would help if you properly indented your code so you can see where the control structures are, it's all a bit random and difficult to read. – Milo Dec 26 '16 at 21:21
  • It is properly indented, just when I copied/pasted, there are some inconsistencies – Trenton Moore Dec 26 '16 at 21:31
  • I'll see if I can clean it up a bit in the comments – Trenton Moore Dec 26 '16 at 21:32
  • I'm not sure that I understand why you need to have two separate loops; one for each half of the alphabet. – Jami Gibbs Dec 26 '16 at 21:34
  • Two different columns on the page – Trenton Moore Dec 26 '16 at 21:36
1

I would approach this a bit differently. You're outputting all pages, so I would just get them all in one go. You can then iterate over each letter, run the loop, and compare the current letter with the page title to see if it matches.

$args = array(
    'post_type' => 'page',
    'posts_per_page' => -1,
    'orderby' => 'title',
    'order' => 'ASC'
);
$all_pages = new WP_Query( $args );

$columns = array(
    range( 'A', 'J' ),
    range( 'K', 'Z' )
);

foreach( $columns as $letters ){

    foreach( $letters as $letter ){

        echo $letter;

        while( $all_pages->have_posts() ){
            $all_pages->the_post();
            $title = get_the_title();
            if( $letter == strtoupper( $title[0] ) ){
                echo $title;
            }
        }
        $all_pages->rewind_posts();
    }

}

wp_reset_postdata();
  • While this looks like it would work fine, it would be hard for me to have to go back and reformat everything. This is on a live website. As it is right now, X, Y, and Z don't have any pages, so I am just excluding them from my loop. I'd rather not have to go back and redo the code if possible. – Trenton Moore Dec 27 '16 at 18:13
  • Mainly what I want is to understand why this would be happening. If the code is working perfectly fine on every other letter, why is it that with X, Y, and Z, it just breaks? I can fix the issue relatively easily, I just have to remove X Y and Z from the loop and add them manually like I was doing previously. I just need to understand the reasoning behind the code breaking. – Trenton Moore Dec 27 '16 at 18:39

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.