对模板图像的引用进行硬编码



我正在与WordPress合作,但是我认为这个问题更通用,因此决定将其发布在此处(请否则建议)。我在函数中具有以下功能。php:

function get_thumbnail(){
    if (has_post_thumbnail()) {
        return get_the_post_thumbnail();
    } else {
        return "<img src='" . get_bloginfo('template_directory') ."/images/thisimage.jpg' alt='This Image'>";
    }
  }

我需要将URL绝对制作,所以我以这种方式重写了:

function get_thumbnail(){
    if (has_post_thumbnail()) {
        return get_the_post_thumbnail();
    } else {
        return "<img src='http://mysite.co.uk/folder/images/thisimage.jpg' alt='This Image'>";
    }
  }

正确吗?

谢谢

get_site_url();将是一种更好的方法。而不是硬编码

function get_thumbnail(){
    if (has_post_thumbnail()) {
        return get_the_post_thumbnail();
    } else {
        return "<img src='" get_site_url()."/". get_bloginfo('template_directory') ."/images/thisimage.jpg' alt='This Image'>";
    }
}

如果您的网站URL更改

,这将阻止您不得不更改代码

最新更新