PHP高级自定义字段:仅在分配给页面时显示项目

时间:2014-07-18 15:07:39

标签: php wordpress advanced-custom-fields

请耐心等待。编程新手。

我正在尝试使用ACF和转发器插件创建餐馆菜单。

下面的屏幕截图显示了我想要实现的目标。这是已经将数据添加到数据库的后端。

enter image description here

这是我获取所需字段的基本循环。

<?php while( have_rows('menus') ): the_row(); ?>

            <?php
                $item_name = get_sub_field('item_name');
                $item_price = get_sub_field('item_price');
                $item_description = get_sub_field('item_description');
                $special = get_sub_field('special');
            ?>

            <h1 class="<?php echo $special"?>>
                 <?php echo $item_name ?>...<?php echo $item_price ?>
            </h1>

            <h2><?php echo $item_description ?></h2>

<?php endwhile; // end of the loop. ?>

我有三个模板,每个模板都显示相同的菜单,但在某些页面中,将显示“特殊”项目,其中一个类设置为在CSS中设置样式。

e.g

King's Cross Page列出了Dish 1和Dish 2(特别)

Covent Garden Page列出了Dish 1并跳过Dish 2,因为它被选为King's Cross的特别之处。

如何跳过未分配给某个模板的字段?

谢谢。

1 个答案:

答案 0 :(得分:0)

是的,仍然不确定我是否完全理解这一点,但如果我做对了,你是否需要检查页面是否等于特殊字段?

类似的东西:

<?php while( have_rows('menus') ) : the_row();
    $item_name = get_sub_field('item_name');
    $item_price = get_sub_field('item_price');
    $item_description = get_sub_field('item_description');
    $special = get_sub_field('special');
    ?>
    <?php if( $special == get_the_title() ) :
        ?>
        <h1 class="<?php echo $special; ?>">
             <?php echo $item_name ?>...<?php echo $item_price ?>
        </h1>
        <h2><?php echo $item_description ?></h2>
        <?php
    endif;
endwhile; // end of the loop. ?>
相关问题