1

我正在尝试根据自定义帖子类型输出不同的文本,并且我收到了一个语法错误,我认为这是由于多个 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 } ?>
}

?>
4

2 回答 2

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;
    }
}
?>
于 2012-10-08T05:12:27.917 回答
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
;
    }
  }
}
于 2012-10-08T05:58:27.193 回答