dongtiao2976
2018-12-10 10:59
浏览 232
已采纳

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

So I attempted to look for a solution: custom wordpress theme: layout images not displaying

It did not work for me.

enter image description here

My directory for my theme is

wp-content/themes/fearnothing/

and consist of these files

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

css folder contains

fearnothing.css

js folder is empty

fearnothing.js

My header.php has the following code:

    <!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;
        }

I'm trying to add the gif but it did not work. so I tried an image instead. I tested my code offline with html and it works fine?

EDIT I added a images folder to my theme where the theme would be located.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • du656637962 2018-12-10 21:20
    已采纳

    So I figured out a method that works which is not really how I wanted it to work.

    I needed to upload the image directly through wordpress media.

    Which is stored in /wp-content/uploads I wanted the images to be inside the themes folder wp-content/themes/fearnothing/images

    but for some odd reason the files are being corrupted. (hence the broken image file)

    After uploading directly to the uploads folder I was able to use the tag I had originally.

    <img  class ="nightsky" src="https://mywesbiteurl.com/wp-content/uploads/2018/12/hrtbrk.png" alt="" >
    
    已采纳该答案
    打赏 评论
  • dosin84644 2018-12-10 11:15

    add Home URL above wp-content

    <img class ="nightsky" src="<?php echo home_url(); ?>/wp-content/themes/fearnothing/hrtbrk.png" alt ="3">
    
    打赏 评论
  • duanbin3021 2018-12-10 11:20

    Try using get_stylesheet_directory_uri() function

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

    Reference link

    打赏 评论
  • duanmao1975 2018-12-10 11:21

    Try this below options.

    <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">
    
    打赏 评论
  • dsfds656545 2018-12-10 11:30

    Try using get_template_directory() function

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

    打赏 评论

相关推荐 更多相似问题