William
William

Reputation: 1030

My WordPress don't display the images that I've hardcoded

I'm coding a site in WordPress and I'd like to have a bookmark star shown before a course name.

The thing is that the code works fine, I can see that it translates to the right place where the image is, but the image appears to not be found.

Is there any configuration I need to do to make this image to show or anything I'm doing wrong?

Code:

$estrela = get_stylesheet_directory() . '/images/estrela.png';
$estrelaFavorito = get_stylesheet_directory() . '/images/estrela-favorito.png';

if ($isFavorito) {
    $img = $estrelaFavorito;
} else {
    $img = $estrela;
}

if (is_user_logged_in()) { ?>
    <div class="add-remove-bookmark" onclick="addToBookmark(<?php echo $user_id ?>, 'course', <?php echo $course_id ?>)" >
        <img src="<?php echo $img; ?>" alt="favoritos" width="20" height="20"/>
    </div>
<?php }

Results in:

<div class="add-remove-bookmark" onclick="addToBookmark(x, x, x)">
    <img src="/home/xxx/www/wp-content/themes/wplms_child/images/estrela-favorito.png" alt="favoritos" width="20" height="20">
</div>

Edit:

If I change to the code below, it works, but not really best practice:

$estrela = 'https://www.xxx.com.br/wp-content/themes/wplms_child/images/estrela.png';
$estrelaFavorito = 'https://www.xxx.com.br/wp-content/themes/wplms_child/images/estrela-favorito.png';

Solution:

I've replaced the variables $estrela and $estrelaFavorito for the code below and now it works!

$estrela = get_stylesheet_directory_uri() . '/images/estrela.png';
$estrelaFavorito = get_stylesheet_directory_uri() . '/images/estrela-favorito.png';

Upvotes: 0

Views: 117

Answers (1)

Bipbip
Bipbip

Reputation: 184

Replace:

//Returns an absolute server path
get_stylesheet_directory()

by

// return theme directory url 
get_stylesheet_directory_uri()

https://codex.wordpress.org/Function_Reference/get_stylesheet_directory_uri

Upvotes: 2

Related Questions