我目前正在为一个管弦乐队的一个网站工作。需要根据其文书列出各个成员。成员有一个自定义的帖子类型的传记,我通过一个自定义字段捕获工具值。
我能弄清楚如何在相关部分显示相关人员的唯一方法是通过自定义post类型一次又一次地循环,通过比较元值来显示演奏特定乐器的人员。
代码如下所示:
<?php $args = array( \'post_type\' => \'biographies\', \'posts_per_page\' => -1 ); ?>
<ul class="no-bull hijax">
<?php $biog = new WP_Query($args);
if( $biog->have_posts() ) : while( $biog->have_posts() ) : $biog->the_post();
$player = get_post_meta($post->ID, \'player\', true);
if ($player == \'yes\') :
$instrument = get_post_meta($post->ID, \'instrument\', true);
if ($instrument == \'violin\') :
?>
<li><a id="artist_id_<?php the_ID(); ?>" class="nb" href="<?php the_permalink(); ?>"><?php the_title(); ?></a> : <?php echo($instrument); ?></li>
<?php
endif;
endif;
endwhile; endif;
wp_reset_query();
$biog = new WP_Query($args);
if( $biog->have_posts() ) : while( $biog->have_posts() ) : $biog->the_post();
$player = get_post_meta($post->ID, \'player\', true);
if ($player == \'yes\') :
$instrument = get_post_meta($post->ID, \'instrument\', true);
if ($instrument == \'viola\') :
?>
<li><a id="artist_id_<?php the_ID(); ?>" class="nb" href="<?php the_permalink(); ?>"><?php the_title(); ?></a> : <?php echo($instrument); ?></li>
<?php
endif;
endif;
endwhile; endif;
wp_reset_query();
$biog = new WP_Query($args);
if( $biog->have_posts() ) : while( $biog->have_posts() ) : $biog->the_post();
$player = get_post_meta($post->ID, \'player\', true);
if ($player == \'yes\') :
$instrument = get_post_meta($post->ID, \'instrument\', true);
if ($instrument == \'cello\') :
?>
<li><a id="artist_id_<?php the_ID(); ?>" class="nb" href="<?php the_permalink(); ?>"><?php the_title(); ?></a> : <?php echo($instrument); ?></li>
<?php
endif;
endif;
endwhile; endif;
wp_reset_query();
等等,等等,等等,等等。(页面上当前有12个循环!!)这显然是完全低效的,但很简单,我不知道如何编写更好的代码,这需要一些帮助!