2017-09-01 55 views
1

我正在研究wordpress主题,我想要自定义标题背景。我曾经使用wordpress提供的add_theme_support('custom-header);函数,但它的工作原理并不是我想要的。我想要一个背景图像而不是img。我做了一个名为header_background_callout()的函数,我添加了一个部分,设置和控制。该功能通过wordpress中的自定义部分工作并输出用户选择的背景url。有没有一种方法可以在CSS中做这样的事情? :background-image: url(<?php echo wp_get_attachment_url(<?php get_theme_mod()); ?>);有没有混合CSS和PHP的方法?

在此先感谢。

+4

的[我如何运行里面的CSS PHP]可能的复制(https://开头stackoverflow.com/questions/12367134/how-do-i-run-php-inside-css) – GrumpyCrouton

回答

0

是好客。
我做了我的CSS文件的PHP文件,并因为我可以做你说

background-image: url(<?php echo get_theme_mod(); ?>); // I suppose you need to echo the mod? 

在HTML,你使用它像;

<Link rel="stylesheet" type="text/css" href="style.php"> 

编辑,你可能需要到PHP文件的标题更改为css文件:

<?php header("content-type: text/css; charset: UTF-8"); ?> 
+0

工作!但有没有办法,我可以使用wp_enqueue_style()而不是链接它在HTML文件中?非常感谢。 –

+0

是的,我忘记了回声和函数之前的wp_get_attachment_url()。 –

+0

我不知道那是什么。但通常我会说你可以在那里回应你想要的任何文字。 – Andreas

1

尝试。 echo“backgroud-img ='url('get_theme_mod()')'”;

1

您可以将内联样式添加到使用你的主题wp_add_inline_style

<?php wp_add_inline_style($handle, $data); ?> 

其中$数据是你想要在你的主题包括CSS。这样你可以将你的php变量作为CSS加载到你的网站上。

0

我发现这个简单的方法和它的作品最好的:

在你的index.php文件中加入:

<?php 

     function bg_img_alt() { 
     if(get_theme_mod('vibe_hero_background_setting') > 0) { 
      return wp_get_attachment_url(get_theme_mod('vibe_hero_background_setting')); 
     }else{ 
      return get_template_directory_uri() . '/img/bg.jpeg'; 
     } 
     } 
?> 

     <style type="text/css"> 

     background: linear-gradient(rgba(0,0,0,.3),rgba(0,0,0,.3)), url(<?php echo bg_img_alt(); ?>); 
     background-repeat: no-repeat; 
     background-position: center; 
     background-size: cover; 
     background-attachment: fixed; 
    </style> 
相关问题