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. ?>
    
    点赞 打赏 评论

相关推荐 更多相似问题