0

I'm trying to make a theme that picks an image from a folder based on the domain name, and if the file does not exist, then place a H3 with the site's title.

My code is:

<?php $server_name = preg_replace('/([wW]{3,3}\.|)\.(es|com|net)/', '',$_SERVER['HTTP_HOST']); $imagen = bloginfo('stylesheet_directory').'/images/logos/'.$server_name.'.png'; if (file_exists(get_template_directory(). '/images/logos/' .$server_name.'.png')) { ?>
   <a href="<?php echo home_url( '/' ); ?>"><img src="<?php echo $imagen ?>" border="0" width="230px" height="60px" alt="<?php bloginfo( 'name' ); ?>"/></a>
  <?php  }else{ ?>
<h3><?php bloginfo( 'name' ); ?></h3>
  <?php } ?>

When I tested it on a responsive theme like:

 <?php if (file_exists(get_template_directory(). '/core/images/logos/'.$nombre_servidor = preg_replace('/([wW]{3,3}\.|)\.(es|com|net)/', '',$_SERVER['HTTP_HOST']).'.png')){ ?>
   <a href="<?php echo home_url( '/' ); ?>"><img src="<?php header_image(); ?>" width="<?php echo get_custom_header()->width; ?>" height="<?php echo get_custom_header()->height; ?>" alt="<?php bloginfo( 'name' ); ?>"/></a>
  <?php  }else{ ?>
<h3><?php bloginfo( 'name' ); ?></h3>
  <?php } ?>

it works like a charm, but I cannot see what I'm doing wrong.

migrated from stackoverflow.com Jun 17 '14 at 13:41

This question came from our site for professional and enthusiast programmers.

  • so what I have to do to change to that category... ? :) – fcsites Jun 17 '14 at 10:09
  • "works like a charm" implies it does work, so what exactly is the issue that you are having? – Rarst Jun 17 '14 at 15:42
  • the first code that I post give me the URL with the www. before, not lile the second one. And the second one did not work at all in the other theme. – fcsites Jun 17 '14 at 15:43
0

Solved with:

<?php if (file_exists(get_template_directory().'/images/logos/'.$server_name = preg_replace('/[wW]{3,3}\.|\.(es|com|net)/', '',$_SERVER['HTTP_HOST']).'.png')) { ?>
   <a href="<?php echo home_url( '/' ); ?>"><img src="<?php bloginfo('stylesheet_directory'); ?>/images/logos/<?php echo $server_name = preg_replace('/[wW]{3,3}\.|\.(es|com|net)/', '',$_SERVER['HTTP_HOST']).'.png' ?>" border="0" width="230px" height="60px" alt="<?php bloginfo( 'name' ); ?>"/></a>
  <?php  }else{ ?>
<h3><?php bloginfo( 'name' ); ?></h3>
  <?php } ?>

Thanks at all!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.