Syntax Error on if-statement

I’m trying to output different text depending on the custom post type and I’m getting a syntax error which I believe is due to multiple if-statements. The problem is that I have a very limited knowledge of PHP. Any ideas?

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

?>

Related posts

Leave a Reply

2 comments

  1. remove opening php tags like:
    change:

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

    to

    <?php if ( 'lettering' == get_post_type() ) {
    
        if( function_exists( 'attachments_get_attachments' ) ) { 
          .......
    

    and similarly in elseif

    Added:

    <?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;
        }
    }
    ?>
    
  2. As MarcB comments – popping in and out of PHP like this is not ideal. The alternative syntax is great if you are writing a predominantly HTML based file with some PHP injection, otherwise I’d use something like a HEREDOC to make things more easy to spot:

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