if语句的语法错误

时间:2012-10-08 05:10:17

标签: php wordpress if-statement

我正在尝试根据自定义帖子类型输出不同的文本,我收到语法错误,我认为这是由多个if语句引起的。问题是我对PHP的知识非常有限。有什么想法吗?

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

?>

2 个答案:

答案 0 :(得分:3)

删除开放的php标记,例如: 改变:

<?php if ( 'lettering' == get_post_type() ) {

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

<?php if ( 'lettering' == get_post_type() ) {

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

,同样在elseif

添加了:

<?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;
    }
}
?>

答案 1 :(得分:0)

正如MarcB的评论 - 像这样弹出进出PHP并不理想。如果您使用PHP注入编写一个主要基于HTML的文件,那么替代语法很棒,否则我会使用类似HEREDOC的东西来让事情更容易被发现:

<?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
;
    }
  }
}