我最近和以前都为我的公司创建了一个网站,我只是简单地手动更改一个变量,并将相同的代码一个接一个地复制和粘贴了26次,但我想我应该循环使用它,使其更易于调试或更改。
下面是循环代码。
<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>
基本上我有两个变量:$alpha\\u char和$first\\u char。循环找到$alpha\\u char的数值,然后if语句将其更改为字母。我知道这有点不合常规,但很管用。除了一个问题。由于某种原因,当它到达x、y和z时,它会无限循环。I\'v甚至尝试将它们从循环中分离出来,看看它是否有某种限制,但它没有。出于某种原因,如果将X、Y或Z设置为$first\\u char,则会出现问题。我把它们全部替换为字母N,没有任何问题,所以看起来只是这些字母的问题。