Trouble showing images in other pages than Home (w

2019-03-02 10:02发布

问题:

Hi I have a small website im doing for a customer and I have used a html/css-site and transferred it to wordpress by using blank theme. So far so good, have a look at energyshop.se if u want and in startpage the top two images are shown, but not udner the rest of the tabs - why? I add the images in the header.php so it should find them on all tabs...?

//header.php

<body <?php body_class(); ?>>

<div id="container">

    <div id="header" onclick="location.href='http://www.energyshop.se/';" style="cursor: pointer;">
        <h1><a href="<?php echo get_option('home'); ?>/"><?php bloginfo('name'); ?></a></h1>
        <div class="description"><?php bloginfo('description'); ?></div>
    </div>
<div id="main_menu">
<?php wp_nav_menu(); ?>
</div>

回答1:

This is because you're using relative paths. When creating a WordPress theme and you want to load resources from your theme, you should use absolute paths. There are two template tags that make this easy for you: get_bloginfo() and bloginfo(). The first one returns the value, and the second one echoes the value(that you request via the first argument passed to the function).

So in order to display an image, you should have:

<img src="<?php bloginfo('stylesheet_directory'); ?>/images/icon_en_global.png" alt="English.png">

This will always result in this(for your site):

<img src="http://energyshop.se/wp-content/themes/blank/images/icon_en_global.png" alt="English.png">

So simply replace all wp-content/themes/blank with <?php bloginfo('stylesheet_directory'); ?>.