繁体   English   中英

WordPress-背景图片无法使用home_url()

[英]Wordpress - background image not working with home_url()

我在WordPress网站上有很多正在使用的背景图片,这些图片准备投入生产。 我正在更改style.css文件中的所有localhost绝对链接。 我从以下代码更改了背景图片的代码:

background: url('http://localhost:8888/wp-content/uploads/2017/07/homepagemain.jpg') center center no-repeat;

对此:

 background: url('<?php echo home_url(); ?>/wp-content/uploads/2017/07/homepagemain.jpg') center center no-repeat;

当我这样做时,图像不会显示出来,不确定我是否使用了错误的功能,但是可以在我的html中的img链接上使用。 我怎样才能解决这个问题? 我是否需要使用get_the_post_thumbnail_url()将图像编辑移至管理区域并远离样式表? 任何帮助表示赞赏。

在模板中使用以下代码。 因为PHP代码在CSS文件中不起作用。 所以你必须在php文件中使用

<?php
$uploads = wp_upload_dir();
$upload_path = $uploads['baseurl']; // now how to get just the directory name?
?>

<div style="background: url('<?php echo esc_url($upload_path); ?>/2017/07/homepagemain.jpg') center center no-repeat;"></div>

我不知道为什么其他答案会造成如此简单,如此复杂的问题。

您需要做的就是从映像路径中删除http://localhost:8888 ,从而创建一个相对链接,它将正常工作。

如果您决定将来进行更改,则还可以在http和https协议上使用它,从而获得更多好处。 所有你需要的是:

background: url('/wp-content/uploads/2017/07/homepagemain.jpg') center center no-repeat;

将图像保存在图像文件夹中,并在模板中使用以下代码:

<div style="background: url('<?php bloginfo('template_url'); ?>/images/homepagemain.jpg') center center no-repeat;"></div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM