dph19153 2014-07-18 15:07
浏览 53
已采纳

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

Please bear with me. New to programming.

I’m trying to create a restaurant menu using ACF and the repeater plugin.

The screenshot below shows what I’m trying to achieve. This is the backend which already adds the data to the database.

enter image description here

This is my basic loop to grab the fields needed.

<?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. ?>

I have three templates each showing the same menu but in certain pages ‘special’ items will be shown where a class is set to be styled in CSS.

e.g

King’s Cross Page lists Dish 1 and Dish 2(special)

but

Covent Garden Page lists Dish 1 and skips Dish 2 because it’s selected to be a special for King’s Cross.

how can I skip fields not assigned to a certain template?

Thank you.

  • 写回答

1条回答 默认 最新

  • dongmieqiao3152 2014-07-18 15:24
    关注

    Right, still not sure if I fully understand this, but if I've got it right, do you need to check if the page is equal to the special field?

    Something like:

    <?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. ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入