【问题标题】:Parse error: syntax error, unexpected 'else' (T_ELSE) in a Wordpress Template [duplicate]解析错误:语法错误,Wordpress 模板中出现意外的“else”(T_ELSE)[重复]
【发布时间】:2015-01-22 19:21:56
【问题描述】:

我正在排除“解析错误:语法错误,意外的 'else'”,我认为问题与我的括号有关。我是 php 新手,所以我不太清楚这里出了什么问题。这是一个 wordpress 页面模板,它将检查用户是否已登录...如果没有,它将显示一条消息,说明您需要登录才能查看该页面。这是产生问题的 .php 模板的代码:

<?php
/*
Template Name: Logged-In Users Page
*/
?>
<!--?php if(is_user_logged_in()):?-->
<?php get_header(); ?>

    <?php
    $sidebar_position = get_post_meta( get_queried_object_id(), 'vw_page_sidebar_position', true );
    $sidebar_position_class = '';
    if ( 'left' == $sidebar_position ) {
    $sidebar_position_class = 'sidebar-left';
    } else if( 'right' == $sidebar_position ) {
    $sidebar_position_class = 'sidebar-right';
    } else {
    $sidebar_position_class = 'sidebar-hidden';
    }
    ?>

<div id="page-wrapper" class="container <?php echo $sidebar_position_class; ?>">
<div class="row">
    <div id="page-content" class="col-sm-7 col-md-8">
        <?php if (have_posts()) : ?>
            <?php while ( have_posts() ) : the_post(); ?>

                <?php get_template_part( 'templates/page-title' ); ?>

                <article id="post-<?php the_ID(); ?>" <?php post_class( 'clearfix' ); ?>>
                    <?php get_template_part( 'templates/post-formats/format', get_post_format() ); ?>
                    <div class="post-content"><?php the_content(); ?></div>
                </article>

            <?php endwhile; ?>

            <?php get_template_part( 'templates/pagination' ); ?>

            <?php comments_template(); ?>

        <?php else : ?>

            <h2><?php _e('Not Found', 'envirra') ?></h2>

        <?php endif; ?>
    </div>

    <?php if ( 'left' == $sidebar_position || 'right' == $sidebar_position ) : ?>
    <aside id="page-sidebar" class="sidebar-wrapper col-sm-5 col-md-4">
        <?php get_sidebar(); ?>
    </aside>
    <?php endif; ?>
</div>
</div>
<?php get_footer(); ?>
<?php else:
wp_die('Sorry, you must first <a href="/wp-login.php">log in</a> to view this page. You can <a href="/wp-login.php?action=register">register free here</a>.');
endif; ?>

【问题讨论】:

  • 第几行?错误应该说
  • &lt;?php else : ?&gt; 缺少其开头等效项。 &lt;?php if (condtion):?&gt; - 如果不使用,请将其删除。
  • 而不是&lt;?php else: wp_die...

标签: php parsing syntax brackets


【解决方案1】:

你已经注释掉了这个 if 语句的第一部分:

<!--?php if(is_user_logged_in()):?-->

应该是:

<?php if(is_user_logged_in()):?>

或者,删除末尾的部分:

<?php else:
wp_die('Sorry, you must first <a href="/wp-login.php">log in</a> to view this page. You can <a href="/wp-login.php?action=register">register free here</a>.');
endif; ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-26
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-02
    • 2013-04-27
    相关资源
    最近更新 更多