skip to Main Content

I’m trying to create a conditional statement to change the href of my header logo according to a certain page number. For example, on page (29) I want the logo to direct to “facebook” and on all other pages I want it to direct to the “home page”.

<div class="logo_container">
    <span class="logo_helper"></span>
  <?php
    if( is_page(29) ) {
    <a href="https://www.facebook.com">
        <img src="<?php echo esc_attr( $logo ); ?>" 
             alt="<?php echo esc_attr( get_bloginfo( 'name' ) ); ?>" 
             id="logo" 
             data-height-percentage="<?php echo esc_attr( et_get_option( 'logo_height', '54' ) ); ?>" />
    </a>
  } else {
    <a href="<?php echo esc_url( home_url( '/' ) ); ?>">
        <img src = "<? php echo esc_attr ($ logo);?>" 
            alt = "<? php echo esc_attr ( get_bloginfo ('name')); ?> "
            id =" logo "
            data-height-percentage =" <? php echo esc_attr (et_get_option ('logo_height', '54')); ?> "/>
    </a>

  }
  ?>
</div>

I get this error

(( ! ) Parse error: syntax error, unexpected ‘<‘ in I:wampwwwtestnomwp-contentthemesdivi-childheader.php on line 244)

3

Answers


  1. You’re not closing and opening php. I think I’ve got it property edited below:

    <div class="logo_container">
                        <span class="logo_helper"></span>
                        <?php
                            if( is_page(29) ) { ?>
                            <a href="https://www.facebook.com">
                                <img src="<?php echo esc_attr( $logo ); ?>" alt="<?php echo esc_attr( get_bloginfo( 'name' ) ); ?>" id="logo" data-height-percentage="<?php echo esc_attr( et_get_option( 'logo_height', '54' ) ); ?>" />
                            </a>
                       <?php } else { ?>
                        <a href="<?php echo esc_url( home_url( '/' ) ); ?>">
                            <img src = "<? php echo esc_attr ($ logo);?>" alt = "<? php echo esc_attr (
                            get_bloginfo ('name')); ?> "id =" logo "data-height-percentage =" <? php echo
                            esc_attr (et_get_option ('logo_height', '54')); ?> "/>
                        </a>
    
                     <?php   }
                        ?>
                    </div>
    
    Login or Signup to reply.
  2. You need to close the php tags at the final of php code. Like in the if.

    <div class="logo_container">
        <span class="logo_helper"></span>
        <?php if (is_page(29)) { ?>
    
            <a href="https://www.facebook.com">
                <img src="<?php echo esc_attr($logo); ?>" alt="<?php echo esc_attr(get_bloginfo(' name')); ?>" id="logo" data-height-percentage="<?php echo esc_attr(et_get_option('logo_height', '54')); ?>">
            </a>
        <?php } else { ?>
            <a href="<?php echo esc_url(home_url('/')); ?>">
                <img src="<?php echo esc_attr($logo); ?>" alt="<?php echo esc_attr(get_bloginfo('name')); ?>" id=" logo " data-height-percentage="<?php echo esc_attr(et_get_option('logo_height', '54')); ?>">
            </a>
        <?php } ?>
    </div>
    
    Login or Signup to reply.
  3. For my knoledge you cannot use php bloques inside another php bloques.
    So when you do the if else echo all html instead.

    <div class="logo_container">
                    <span class="logo_helper"></span>
                    <?php
                        if( is_page(29) ) {
                            echo '<a href="https://www.facebook.com">
                            <img src="' . esc_attr($logo) . '" alt="' . esc_attr(get_bloginfo('name')) . '" id="logo" data-height-percentage="' . esc_attr(et_get_option('logo_height', '54')) . '" />
                            </a>';
                            } else {
                                echo '<a href="' . esc_url(home_url('/')) . '">
                                <img src = "' . esc_attr ($logo) . '" alt = "' .  esc_attr(get_bloginfo('name')) .' "id =" logo "data-height-percentage ="' . esc_attr (et_get_option('logo_height', '54')) . '"/>
                            </a>';
                        }
                    ?>
                </div>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search