首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >if-statement上的语法错误

if-statement上的语法错误
EN

Stack Overflow用户
提问于 2012-10-08 13:10:17
回答 2查看 136关注 0票数 1

我试图根据自定义的post类型输出不同的文本,但我得到了一个语法错误,我认为这是由于多个if语句造成的。问题是我对PHP的了解非常有限。有什么想法吗?

代码语言:javascript
复制
<?php

if ( 'lettering' == get_post_type() ) {

    <?php if( function_exists( 'attachments_get_attachments' ) ) { 
            $attachments = attachments_get_attachments();
            $total_attachments = count( $attachments );
            if( $total_attachments ) : ?>
            <ul id="process"><span>Process:</span>
            </ul>
            <br>
                <?php endif; ?> <?php } ?>

} elseif ( 'type' == get_post_type() ) {

    <?php if( function_exists( 'attachments_get_attachments' ) ) { 
            $attachments = attachments_get_attachments();
            $total_attachments = count( $attachments );
            if( $total_attachments ) : ?>
            <ul id="process"><span>Additional Shots</span>
            </ul>
            <br>
                <?php endif; ?> <?php } ?>
}

?>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-08 13:12:28

删除php开头的标签,如: change:

代码语言:javascript
复制
<?php if ( 'lettering' == get_post_type() ) {

    this one --> <?php if( function_exists( 'attachments_get_attachments' ) ) { 

代码语言:javascript
复制
<?php if ( 'lettering' == get_post_type() ) {

    if( function_exists( 'attachments_get_attachments' ) ) { 
      .......

elseif中也是类似的

添加:

代码语言:javascript
复制
<?php
if ( 'lettering' == get_post_type() ) {
    if( function_exists( 'attachments_get_attachments' ) ) { 
        $attachments = attachments_get_attachments();
        $total_attachments = count( $attachments );
        if( $total_attachments ): 
?>
                <ul id="process"><span>Process:</span>
                </ul>
                <br>
<?php 
        endif; 
    }
} else if ( 'type' == get_post_type() ) {
    if( function_exists( 'attachments_get_attachments' ) ) { 
        $attachments = attachments_get_attachments();
        $total_attachments = count( $attachments );
        if( $total_attachments ): 
?>
                <ul id="process"><span>Additional Shots</span>
                </ul>
                <br>
<?php 
        endif;
    }
}
?>
票数 3
EN

Stack Overflow用户

发布于 2012-10-08 13:58:27

像这样弹出MarcB注释是不理想的。如果你正在用一些PHP注入编写一个主要基于HTML的文件,那么另一种语法是很棒的,否则我会使用HEREDOC这样的东西来让事情更容易识别:

代码语言:javascript
复制
<?php

if ( 'lettering' == get_post_type() ) {
  if( function_exists( 'attachments_get_attachments' ) ) {
    $attachments = attachments_get_attachments();
    $total_attachments = count( $attachments );

    if( $total_attachments ) {
      echo <<<EOSTRING

      <ul id="process"><span>Process:</span>
      </ul>
      <br>

EOSTRING
;
    }
  }
} elseif ( 'type' == get_post_type() ) {
  if( function_exists( 'attachments_get_attachments' ) ) {
    $attachments = attachments_get_attachments();
    $total_attachments = count( $attachments );

    if( $total_attachments ) {
      echo <<<EOSTRING

      <ul id="process"><span>Additional Shots</span>
      </ul>
      <br>

EOSTRING
;
    }
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12775634

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档