自定义wordpress主题,图片不显示?

时间:2018-12-10 10:59:46

标签: php html css image gif

所以我试图寻找一种解决方案: custom wordpress theme: layout images not displaying

它对我不起作用。

enter image description here

我的主题目录为

wp-content/themes/fearnothing/

并且由这些文件组成

/css(folder) 
/js (folder)
/images (folder)
header.php
index.php
function.php
footer.php
style.css
hrtbrk.gif
hrtbrk.png

css文件夹包含

fearnothing.css

js文件夹为空

fearnothing.js

我的header.php具有以下代码:

    <!DOCTYPE html>
    <html>
    <head>
        <title>example title</title>
        <?php wp_head(); ?>
    </head>

    <body>

<img class ="nightsky" src="wp-content/themes/fearnothing/hrtbrk.png" alt ="3">

functions.php

<?php 
    function fearnothing_script_enqueue(){
        wp_enqueue_style('customstyle',  get_template_directory_uri().'/css/fearnothing.css',array(), '1.1.2', 'all');


    }

    add_action('wp_enqueue_scripts', 'fearnothing_script_enqueue');

fearnothing.css

html,body{
        background: black;
        color: #8c0707;
        font-family: Courier,Courier New,Lucida Sans Typewriter,Lucida Typewriter,monospace;
        font-size: 10px;
        cursor: pointer;
    }
    .nightsky{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 500px;
    height: 500px;
        }

我正在尝试添加gif,但是没有用。所以我尝试了一张图片。我使用html离线测试了我的代码,效果很好?

编辑 我在主题所在的位置添加了一个图像文件夹。

5 个答案:

答案 0 :(得分:0)

在wp-content上方添加Home URL

<img class ="nightsky" src="<?php echo home_url(); ?>/wp-content/themes/fearnothing/hrtbrk.png" alt ="3">

答案 1 :(得分:0)

尝试使用get_stylesheet_directory_uri()函数

<img class="nightsky" src="<?php echo get_stylesheet_directory_uri()?>/hrtbrk.png" alt="">

引用link

答案 2 :(得分:0)

在以下选项中尝试此操作。

<img class ="nightsky" src="<?php echo site_url(); ?>/wp-content/themes/fearnothing/hrtbrk.png" alt ="3">

OR 

<img class ="nightsky" src="<?php echo home_url(); ?>/wp-content/themes/fearnothing/hrtbrk.png" alt ="3">

答案 3 :(得分:0)

尝试使用get_template_directory()函数

<img class="nightsky" src="<?php echo get_stylesheet_directory_uri()?>/hrtbrk.png" alt="">

答案 4 :(得分:0)

所以我想出了一种行之有效的方法,但这并不是我想要的。

我需要直接通过wordpress媒体上传图片。

哪个存储在/wp-content/uploads中 我希望图像位于主题文件夹wp-content/themes/fearnothing/images

但是由于某些奇怪的原因,文件已损坏。 (因此损坏的图像文件)

直接上传到上载文件夹后,我可以使用原来的标签。

<img  class ="nightsky" src="https://mywesbiteurl.com/wp-content/uploads/2018/12/hrtbrk.png" alt="" >