简体   繁体   中英

Link main logo to custom URLs instead of homepage, with WordPress Avada child theme?

I'm trying to customize Avada to link the main site logo to a custom URL when you're on certain pages. I copied logo.php into the child theme and added this:

        <?php $standard_logo = Avada()->images->get_logo_image_srcset( 'logo', 'logo_retina' ); ?>
        <!-- custom standard logos -->
        <?php
        //Binghamton
        if (is_page( array (194, 376, 534, 329, 499, 489, 479, 476, 467)))
        {
          $standard_logo['srcset'] = '/wp-content/uploads/2018/06/broadway-binghamton-logo.png';
          $standard_logo['url'] = $standard_logo['srcset'];
          $retina_logo['srcset'] = '/wp-content/uploads/2018/06/broadway-binghamton-logo-retina.png';
          $retina_logo['url'] = $standard_logo['srcset'];
        }

        //Erie
        if (is_page( array (248, 382, 589, 926, 1542, 1537, 1514)))
        {
          $standard_logo['srcset'] = '/wp-content/uploads/2018/06/broadway-erie-logo.png';
          $standard_logo['url'] = $standard_logo['srcset'];
          $retina_logo['srcset'] = '/wp-content/uploads/2018/06/broadway-erie-logo.png';
          $retina_logo['url'] = $standard_logo['srcset'];
        }
        ?>
        <!-- standard logo -->
        <img src="<?php echo esc_url_raw( $standard_logo['url'] ); ?>" srcset="<?php echo esc_attr( $standard_logo['srcset'] ); ?>" width="<?php echo esc_attr( $standard_logo['width'] ); ?>" height="<?php echo esc_attr( $standard_logo['height'] ); ?>"<?php echo $standard_logo['style']; // WPCS: XSS ok. ?> alt="<?php echo esc_attr( $logo_alt_attribute ); ?>" retina_logo_url="<?php echo esc_url_raw( $standard_logo['is_retina'] ); ?>" class="fusion-standard-logo" />

        <?php
        if ( Avada()->settings->get( 'mobile_logo', 'url' ) && '' !== Avada()->settings->get( 'mobile_logo', 'url' ) ) {
            $mobile_logo = Avada()->images->get_logo_image_srcset( 'mobile_logo', 'mobile_logo_retina' );
        ?>
            <!-- custom mobile logos -->
            <?php
            //Binghamton
            if (is_page( array (194, 376, 534, 329, 499, 489, 479, 476, 467)))
            {
              $mobile_logo['srcset'] = '/wp-content/uploads/2018/06/broadway-binghamton-logo.png';
              $mobile_logo['url'] = $mobile_logo['srcset'];
            }

            //Erie
            if (is_page( array (248, 382, 589, 926, 1542, 1537, 1514)))
            {
              $mobile_logo['srcset'] = '/wp-content/uploads/2018/06/broadway-erie-logo.png';
              $mobile_logo['url'] = $mobile_logo['srcset'];
            }
            ?>
            <!-- mobile logo -->
            <img src="<?php echo esc_url_raw( $mobile_logo['url'] ); ?>" srcset="<?php echo esc_attr( $mobile_logo['srcset'] ); ?>" width="<?php echo esc_attr( $mobile_logo['width'] ); ?>" height="<?php echo esc_attr( $mobile_logo['height'] ); ?>"<?php echo $mobile_logo['style']; // WPCS: XSS ok. ?> alt="<?php echo esc_attr( $logo_alt_attribute ); ?>" retina_logo_url="<?php echo esc_url_raw( $mobile_logo['is_retina'] ); ?>" class="fusion-mobile-logo" />
        <?php } ?>

which changes out the logos to match the correct cities, but I need the city logos to link to the city pages, instead of the homepage.

http://nac.flywheelsites.com/broadway-in-binghamton/ displays the Binghamton logo but links back to http://nac.flywheelsites.com/ currently.

I tried:

$standard_logo['/broadway-in-binghamton/'] = $standard_logo['srcset'];

but I don't think that's right. Similar questions say to go into the header.php file and find your logo class, but this theme doesn't store it there. Is there a way to insert custom URLs right within the logo.php file?

The code you provided is only about the <img> . When i check your website, i see that both logo's (normal & retina) are wrapped in a link: <a class="fusion-logo-link" href="http://nac.flywheelsites.com/"> . You need to find the source of that link (probably header.php).

OR

Avada is a massive theme, there's probably a filter for the logo link you can use to change it. I don't use Avada that much, I suggest you ask them.


UPDATE, after finding the correct Avada logo link filter avada_logo_anchor_tag_attributes

add_filter('avada_logo_anchor_tag_attributes', 'broadway_logo_link_modify');
function broadway_logo_link_modify() {

  $link = esc_url( home_url( '/' ) );

  if (is_page( array (248, 382, 589, 926, 1542, 1537, 1514))) {
    $link = 'http://nac.flywheelsites.com/broadway-in-binghamton/';
  }

  // another option with which you don't have to add every page id
  $current_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
  if (strpos($current_url, 'http://nac.flywheelsites.com/broadway-in-binghamton/') !== false) {
    $link = 'http://nac.flywheelsites.com/broadway-in-binghamton/';
  }

  $link_arr = array(
    'class' => 'fusion-logo-link',
    'href' => $link,
  );

  return $link_arr;

}

Ofcourse you have to extend the snippet to fit your needs, but you'll get the idea. Place it in your functions.php (not logo.php).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM