Wordpress: Image placed in header.php some in the internal pages

Asked

Viewed 53 times

0

In the header.php I put for example

<img src="wp-content/themes/meu_tema/asserts/imagens/logo.png" title=" />

Home page works, but internal does not work.

How to get around this?

1 answer

0


You are using a relative URL, so when you exit the home page,wp-content/themes/meu_tema/asserts/imagens/logo.png is being added to the URL of the page you are on, so it doesn’t work.

The quick (and dirty) way to solve is to add a / at the beginning of the URL:

<img src="/wp-content/themes/meu_tema/asserts/imagens/logo.png" title=" />

The most correct way is to use the functions get_template_directory_uri() or get_stylesheet_directory_uri(), thus:

<img src="<?php echo esc_url( get_template_directory_uri(). '/asserts/imagens/logo.png' ); ?>" title="" />
<img src="<?php echo esc_url( get_stylesheet_directory_uri(). '/asserts/imagens/logo.png' ); ?>" title="" />
  • Thanks, but he doesn’t concatenate with /asserts/imagens/logo.png and the output is: <img src="http://www.meusite.com.br/wp-content/themes/meu_tema" title="Meu Site" alt="Meu Site">, what can be?

  • Whoa, my fault. I switched the commas for points there and now it should work.

  • 1

    Ricardo, now it worked. And also "reigning" before seeing his answer, I got so: <img src="<?php echo get_template_directory_uri(); ?>/asserts/imagens/logo.png" title="<?php bloginfo('name') ?>" alt="<?php bloginfo('name') ?>" />. Your code is more elegant. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.