if / else with foreach和if statement inside - Parse error:syntax error,unexpected'}'

时间:2015-12-01 14:31:25

标签: php if-statement foreach

我不断在这段代码上遇到语法错误。

<?php 

$day = get_field_object('day_of_the_week');
$value = $day['value'];
$choices = $day['choices'];

    if( $value ): ?>
        <ul>
            <?php foreach( $value as $v ): ?>
                <li>
                    <?php if( $v != $page_title ) { ?>
                        <a class="btn" href="/venture-centre/activities-2/<?php echo $choices[ $v ]; ?>#<?php echo basename(get_permalink()); ?>">
                            <?php echo $choices[ $v ]; ?>
                        </a>
                        <?php } ?>
                </li>
                <?php endforeach; ?>
        </ul>

        <?php } else { ?>
                    <p>This activity is only available on <?php echo $page_title ?>.</p>
        <?php } ?>

        <?php endif; ?>

3 个答案:

答案 0 :(得分:0)

您正在混合标准和替代语法,这是不允许的:

if( $value ): ?>   // start alternative syntax block
    <?php } else { ?>  // where did this get opened?

答案 1 :(得分:0)

你有一个没有开口的大括号。

<?php } ?>

后的</a>

您必须删除此行或在每个

之后添加<?php { ?>

答案 2 :(得分:0)

感谢所有评论,我已做出建议的更改,并意识到我需要elseif代替else来完成我想要实现的目标:

<?php 

$day = get_field_object('day_of_the_week');
$value = $day['value'];
$choices = $day['choices'];

if( count($value) > 1 ) { ?>
    <ul>
        <?php foreach( $value as $v ) { ?>
            <li>
                <?php if( $v != $page_title ) { ?>
                    <a class="btn" href="/venture-centre/activities-2/<?php echo $choices[ $v ]; ?>#<?php echo basename(get_permalink()); ?>">
                        <?php echo $choices[ $v ]; ?>
                    </a>
            </li>
        <?php } } } elseif( count($value) == 1 ) { ?>
                        <p>This activity is only available on <?php echo $page_title; ?>.</p>
        <?php } ?>