Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

wordpress - Why does an image in "header.php" only display in one page?

UPDATE: I have just figured it out! I used a full URL (http://localhost/practice/wp-content/themes/cpmock/assets/images/cplogo.png) as the img src and now it works on all pages.

I hope someone can help.

I am creating a wordpress site/theme, from scratch, for the first time.

I have created a header.php file which contains a logo, some nav links and a search bar.

I have created several files/pages, such as; index.php, front-page.php, page-about.php and single.php for example.

Each file/page calls the header.php and displays it. However, only the front-page.php file shows the image.

All files/pages are in the root folder. The image src is the full file path. I don't understand why the image only works in the front-page.php and not the others.

I hope someone can advise why this is. I have included code from header.php, front-page.php and page-about.php.

header.php:

<!DOCTYPE html>
<html <?php language_attributes() ?>>
<head>
<meta charset="<?php bloginfo('charset')?>">
<meta name="description" content="<?php bloginfo('description')?>">
<title><?php bloginfo('name')?></title>
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<div class="container">
    <header class="header">
        <a href="http://localhost/practice/"><img src="wp- 
        content/themes/cpmock/assets/images/cplogo.png" alt="CP Logo"></a>
        <div class="search-header">
            <?php get_search_form(); ?>
        </div>
        <?php wp_nav_menu( array(
            'theme_location' => 'primary'
        ) ); ?> 
    </header>

front-page.php:

<?php get_header(); ?>
    
    <section class="front-page">
        <h1>front-page.php</h1>
        <p>This is a mock website.</p>
    </section>

<?php get_footer(); ?>

page-about.php:

<?php get_header(); ?>
    
    <section class="page-about">
        <h1>page-about.php</h1>
        <h4>This is my custom about page.</h4>
    </section>

<?php get_footer(); ?>
    

Here are a couple of screenshots. One showing the front-page.php which displays the image. The other of the page-about.php which doesn't show the image.

front-page.php image visible

page-about.php image not visible

question from:https://stackoverflow.com/questions/65626092/why-does-an-image-in-header-php-only-display-in-one-page

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should not type static image URLs like that:

"http://localhost/practice/wp-content/themes/cpmock/assets/images/cplogo.png"

The reason you don't type them like that is that you will have to retype them when you connect to the website to a domain. A better solution would be...

<?php

$imageUrl = get_site_url()."/folder/folder/image.png";

?>


 <a href=""><img src="<?php echo $imageUrl ; ?>" alt="CP Logo"></a>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...