ACF转发器字段未显示在第二个帖子中

时间:2018-07-19 07:46:02

标签: php advanced-custom-fields

我使用的网站主要基于ACF,该网站基于WordPress构建。我目前正在尝试通过添加“费率和季节”自定义帖子类型来添加新功能。基本上有数百个属性都相同的日期范围和季节,但价格不同。

因此,我设置的是带有重复字段的自定义帖子类型,以输入日期范围和季节,因此当需要更新日期时,我们只需要更新“费率”帖子,而不是更新120多个单独的属性。对于网站的其他方面(条款和条件,邻里等等),我已经做了类似的事情。不幸的是……我很难让这个工作。选择自定义帖子类型时,转发器字段中没有任何数据显示。我已经阅读了一个论坛和ACF指南,并尝试了一些修复程序,但现在我真的需要新的眼光了。

以下是自定义帖子类型的代码:

register_field_group(array (
'id' => 'acf_rates',
'title' => 'Rates',
'fields' => array (array (
    'key' => 'field_5a8fdfe319dec',
    'label' => 'Rates per Date Ranges',
    'name' => 'rates_per_date_template',
    'type' => 'repeater',
    'sub_fields' => array (
    array ( 'key' => 'field_5a8fe01d19ded',
        'label' => 'Starting Date',
        'name' => 'rates_start',
        'type' => 'date_picker',
        'instructions' => '',
        'required' => 0,
        'conditional_logic' => 0,
        'wrapper' => array('width' => '','class' => '','id' => '',),
        'display_format' => 'm/d/Y',
        'return_format' => 'Ymd',
        'first_day' => 0,
    ),
    array ( 'key'   => 'field_5a8fe06b19dee',
        'label' => 'Ending Date',
        'name'  => 'rates_end',
        'type' => 'date_picker',
        'instructions' => '',
        'required' => 0,
        'conditional_logic' => 0,
        'wrapper' => array('width' => '','class' => '','id' => '',),
        'display_format' => 'm/d/Y',
        'return_format' => 'Ymd',
        'first_day' => 0,
    ),
    array(  'key' => 'field_5b47bf4e86700',
        'label' => 'rates seasons',
        'name' => 'rates_seasons',
        'type' => 'select',
        'instructions' => '',
        'required' => 0,
        'conditional_logic' => 0,
        'wrapper' => array('width' => '33','class' => '','id' => '',),
        'choices' => array('Low' => 'Low',
                   'Low and Mid' => 'Low and Mid',
                'Mid' => 'Mid',
                'High' => 'High',
                'Holiday' => 'Holiday', ),
        'default_value' => array(   ),
        'allow_null' => 0,
        'multiple' => 0,
        'ui' => 0,
        'ajax' => 0,
        'return_format' => 'value',
        'placeholder' => '',    ),
    ),
    'row_min' => '',
    'row_limit' => '',
    'layout' => 'table',
    'button_label' => 'Add Date Range',
),),

以下是在属性中调用自定义帖子类型的代码:

array(  'key' => 'field_5b47cd1a17dfc',
    'label' => 'Pick Rates Template',
    'name' => 'rates_template',
    'type' => 'relationship',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => array(
    array(  array(  'field' => 'field_5b47cccb17dfb',
            'operator' => '==',
            'value' => 'Yes',
    ),  ),  ),
    'wrapper'   => array(   'width' => '', 'class' => '', 'id' => '', ),
    'post_type' => array(   0 => 'rates',   ),
    'taxonomy'  => array(),
    'filters'   => array(   0 => 'search',
                    1 => 'post_type',       ),
    'elements'  => '',
    'min'       => '',
    'max'       => '',
    'return_format' => 'id',
),

现在这是问题所在。该帖子注册了我在“关系”字段中选择的自定义帖子类型。但是这些行中没有任何数据显示。这是代码的最后一部分:

$low      = get_field('base_low_season_price');
$mid      = get_field('base_mid_season_price');
$high     = get_field('base_high_season_price');
$holiday  = get_field('base_holiday_season_price');
$temp_rates = get_field('rates_template')

if ($rates || $rental_rates || $rental_rands) : 
echo '<a name="Rates"></a><br />';
  if ($temp_rates) :
    echo'<h2 class="blue">Rental Rates & Seasons
             <a href="#Top" class="totop">Back to Top</a></h2>
<table class="rates" width="75%" cellpadding="0" cellspacing="0" cellcolor="#b7d8f9">
<tbody><tr><th>Starting</th><th>Ending</th><th>Season</th><th>Rate</th></tr>';
if(get_field('rental_rates')) {$rates = implode(get_field('rates'));}
        $rates_rows = get_field('rates_per_date_template',$rates);
if( have_rows('rates_per_date_template',$temp_rates) ):
    while( have_rows('rates_per_date_template',$temp_rates) ) : the_row();
        $rates_rows     = get_row('rates_per_date_template');
            $rates_start    = get_sub_field('rates_start');
            $rates_end  = get_sub_field('rates_end');
            $rates_seasons  = get_sub_field('rates_seasons');
    foreach ($temp_rates as $temp_rate) :
        $currentdate = date("Ymd",mktime(0,0,0,date("m"),date("d")-1,date("Y")));
        $starting = $rates_start;
    $sy = substr($starting, 0, 4);
    $sm = substr($starting, 4, 2);
    $sd = substr($starting, 6, 2);
    $tstime = strtotime("{$sd}-{$sm}-{$sy}");

    $ending = $rates_end;
    $ey = substr($ending, 0, 4);
    $em = substr($ending, 4, 2);
    $ed = substr($ending, 6, 2);
    $tetime = strtotime("{$ed}-{$em}-{$ey}");

    if ($ending >= $currentdate)                            
    {echo   '<tr>   <td>'.date('F jS, Y',$tstime).'</td>
            <td>'.date('F jS, Y',$tetime).'</td>
            <td>'.$rates_seasons.'</td>';
        switch ($rates_seasons) {
            case "Low": 
            echo '<td>'.$low.'</td>';
            break;
            case "Low and Mid": 
            echo '<td>'.$low.'</td>';
            break;
            case "Mid": 
            echo '<td>'.$mid.'</td>';
            break;
            case "High":    
            echo '<td>'.$high.'</td>';
            break;
            case "Holiday": 
            echo '<td>'.$holiday.'</td>';
            break;
        }
        echo '</tr>';
        }
        endforeach;
        endwhile;
    endif;
    elseif ($rental_rates) :
echo '<h2 class="blue">Rental Rates & Seasons<a href="#Top" class="totop">Back to Top</a></h2>
<table class="rates" width="75%" cellpadding="0" cellspacing="0" cellcolor="#b7d8f9"><tbody>
<tr><th>Starting</th><th>Ending</th><th>Rate</th></tr>';
$currentdate = date("Ymd",mktime(0,0,0,date("m"),date("d")-1,date("Y")));
foreach ($rental_rates as $rental_rate) :
    $starting = $rental_rate['starting'];
    $sy = substr($starting, 0, 4);
        $sm = substr($starting, 4, 2);
        $sd = substr($starting, 6, 2);
    $stime = strtotime("{$sd}-{$sm}-{$sy}");

        $ending = $rental_rate['ending'];
        $ey = substr($ending, 0, 4);
        $em = substr($ending, 4, 2);
        $ed = substr($ending, 6, 2);
    $etime = strtotime("{$ed}-{$em}-{$ey}");
    if ($ending >= $currentdate) 
    {   echo    '<tr>   <td>'.date('F jS, Y',$stime).'</td>
                    <td>'.date('F jS, Y',$etime).'</td>
                <td>'.$rental_rate['price'].'</td></tr>';
    }
    endforeach;
    endif;
echo '</tbody></table>';
endif;

呈现日期的代码在被单个属性输入的日期使用时才起作用(它使今天之前的所有日期都在前端消失)。另外,switch语句也有效,我使用各个属性的本地日期范围对其进行了测试,并且显示效果很好。但是,一旦我尝试在自定义帖子类型上从中继器调用数据,代码就会停止工作,而我得到的只是表标题行标签,而没有其他任何内容。如果有人可以帮我看看这个,让我知道我做错了,我将不胜感激!!!!

1 个答案:

答案 0 :(得分:0)

好的,感谢@PeterHvD的帮助,我已经完成了代码!这是下面的新部分!

if ($temp_rates):
    echo'<h2 class="blue">Rental Rates & Seasons<a href="#Top" class="totop">Back to Top</a></h2>
        <table class="rates" width="75%" cellpadding="0" cellspacing="0" cellcolor="#b7d8f9"><tbody>
        <tr><th>Starting</th><th>Ending</th><th>Season</th><th>Rate</th></tr>';
        $posts = $temp_rates;
        foreach( $posts as $post ) : setup_postdata( $post );
        if (have_rows('rates_per_date_template',$post->ID) ):
            while( have_rows('rates_per_date_template',$post->ID) ): the_row();
                    $rates_start    = get_sub_field('rates_start');
                    $rates_end      = get_sub_field('rates_end');
                    $rates_seasons  = get_sub_field('rates_seasons');
                    $currentdate = date("Ymd",mktime(0,0,0,date("m"),date("d")-1,date("Y")));
                        $starting = $rates_start;
                        $sy = substr($starting, 0, 4);
                        $sm = substr($starting, 4, 2);
                        $sd = substr($starting, 6, 2);
                        $tstime = strtotime("{$sd}-{$sm}-{$sy}");
                        $ending = $rates_end;
                        $ey = substr($ending, 0, 4);
                        $em = substr($ending, 4, 2);
                        $ed = substr($ending, 6, 2);
                        $tetime = strtotime("{$ed}-{$em}-{$ey}");
                        if ($ending >= $currentdate)                            
                        {   echo '<tr>  <td>'.date('F jS, Y',$tstime).'</td>
                                        <td>'.date('F jS, Y',$tetime).'</td>
                                        <td>'.$rates_seasons.'</td>';
                            switch ($rates_seasons) {
                                case "Low":     
                                    echo '<td>'.$low.'</td>';
                                    break;
                                case "Low and Mid": 
                                    echo '<td>'.$low.'</td>';
                                    break;
                                case "Mid": 
                                    echo '<td>'.$mid.'</td>';
                                    break;
                                case "High":    
                                    echo '<td>'.$high.'</td>';
                                    break;
                                case "Holiday": 
                                    echo '<td>'.$holiday.'</td>';
                                    break;
                            }
                            echo '</tr>';
                        }
            endwhile;
            endif;
        endforeach;
        wp_reset_query();

因此,我必须对关系字段进行完整标记,包括将$ posts重置为自定义帖子类型,然后为所选的自定义帖子类型(费率)调用帖子ID,然后然后通过“ have_rows,the_rows”循环调用转发器字段。我跳过了中间步骤,并错误地假设通过调用关系字段并将其设置为返回ID的方式,即$ temp_rates可以代替$ post-> ID。它现在就在工作!再次感谢彼得!