2016-10-15 1 views
0

私は現時点でWordPressを学んでおり、カスタムフィールド/ポストタイプを使ってWordPressの仕組みを見ています。このループを作成する方法(WordPress/PHP)

ここでは、カスタムフィールドラベルとカスタムフィールドを吐き出すために自分自身を繰り返していることがわかります。

<?php if($query->have_posts()) : while($query->have_posts()) : $query->the_post(); ?> 

     <?php $field_name = "about"; 
     $field = get_field_object($field_name);?> 
     <h5 class="cv-subtitle"><?php echo $field['label'];?></h5> 
     <p class=""><?php the_field('about'); ?></p> 

     <?php $field_name = "work_experience"; 
     $field = get_field_object($field_name);?> 
     <h5 class="cv-subtitle"><?php echo $field['label'];?></h5> 
     <p class=""><?php the_field('work_experience'); ?></p> 

     <?php $field_name = "education"; 
     $field = get_field_object($field_name);?> 
     <h5 class="cv-subtitle"><?php echo $field['label'];?></h5> 
     <p class=""><?php the_field('education'); ?></p> 

     <?php $field_name = "skills"; 
     $field = get_field_object($field_name);?> 
     <h5 class="cv-subtitle"><?php echo $field['label'];?></h5> 
     <p class=""><?php the_field('skills'); ?></p> 

    <?php endwhile; endif; wp_reset_postdata(); ?></div> 

これは、私のポートフォリオの「概要」ページで、CVのスタイルで表示されます。

私はこのように自分自身を繰り返すことを知っていますが、これを防ぐためには良い練習ではなく、何らかのループが必要であることを理解しています。

誰かがこのループを作成したり、ある方向に向いてくれたら、それは素晴らしいことです。

おかげ

答えて

0
<?php if($query->have_posts()) : while($query->have_posts()) : $query->the_post(); ?> 

    <?php 
     $field_names = ['about','work_experience','education','skills']; 
     foreach($field_names as $field_name) 
     { 
     $field = get_field_object($field_name); ?> 
     <h5 class="cv-subtitle"><?php echo $field['label'];?></h5> 
     <p class=""><?php the_field($field_name); ?></p> 
     <?php 
     } 
    ?> 
<?php endwhile; endif; wp_reset_postdata(); ?></div> 
+0

美しい、ありがとうございました! –

関連する問題