やわらかしかっけい

なんでもすぐに忘れてしまう自分のために

WordPress:カスタムフィールドテンプレートでフィールドをグループ化し、追加可能にする

とっても便利なカスタムフィールドテンプレートちゃん

フィールドをグループ化して、投稿者が追加できるようにしたい

カスタムフィールドテンプレートのグループ化の使い方。

プラグインの設定はこうして、、

[field0]
type = fieldset_open
legend = 日付
multipul = true
multipleButton = true
 
[day]
type = textfield
size = 35
label = 日付
blank = true
 
[week]
type = radio
value = mon # tue # wed # thu # fri # sat # sun
default = mon
label = 曜日
 
[field0]
type = fieldset_close

で、出力はこうする

<?php
$day= post_custom('day');
$week= post_custom('week');
$field0 = post_custom('field0');
if ( !empty( $field0 ) ): ?>
<div class="date">
<?php if( $field0 == 1 ){
        echo $day.'<span>'.$week.'</span>';
      }else{
        for( $i = 0; $i < $field0; $i++ ){
          echo $day[$i].'<span>'.$week[$i].'</span>';
          if( $i < $field0 -1 ){ echo '-'; }
        }
      } ?>
</div>
<?php endif; ?>

なぜか削除ボタンが表示されないな・・。

追記:画像も含めたい場合

こうすれば動く。
wp_get_attachment_image_srcでは配列の変数(添字っていうらしい)を[0]にしなきゃいけないのがポイント

<?php
	$atitle= post_custom('a_title');
	$aurl= post_custom('a_url');
	$aimage = post_custom('a_image');
	$field0 = post_custom('field0');
	if ( !empty( $field0 ) ): 
	 if( $field0 == 1 ){
	        echo '<a href="'.$aurl.'" target="_blank">'.$atitle.'<img src="'.$aimage[0].'" alt="'.$atitle.'" /></a>';
	      }else{
	        for( $i = 0; $i < $field0; $i++ ){
	        $aimageurl = wp_get_attachment_image_src($aimage[$i],'full' );	//ここ重要!
	        echo '<a href="'.$aurl[$i].'" target="_blank">'.$atitle[$i].'<img src="'.$aimageurl[0].'" alt="'.$atitle[$i].'" /></a>';
	          if( $i < $field0 -1 ){ echo ''; }
	        }
	      }
	?>
	<?php endif; ?>