douji6940
douji6940
2012-11-21 09:49

如何在内联html中使用变量路径获取背景图像?

  • php
  • background-image
  • wordpress
已采纳

I have my own wordpress template with option setting. My option setting having option for background Image path. I saved this option value in variable .Now how i can add this option setting value to get background Image for specific div class?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dos71253 dos71253 9年前

    You'll need a page that you can put both CSS and PHP on, like header.php in your template. Be sure to note that this CSS hack is there so that you can re-add it when you update the theme.

    Now, in your header.php file, just before the </head> tag, add this:

    <style type='text/css'>
        /* div selector */ {
            background: url(<?php echo get_option("option_name"); ?>);
        }
    </style>
    </head>
    

    You'll need to add a CSS selector to select the div you want to have tha background image, and you'll need to make sure that the background image links correctly. You may need to add /wp-content/ or similar things to the CSS or the option value if the image doesn't link correctly at first.

    点赞 评论 复制链接分享

为你推荐