Dynamic CSS Background Image in Wordpress

I just installed WordPress and tried to install the theme. All is well, but for one simple problem. I know how to make the image a dynamic image at the beginning to set my index.php, for example:

<img src="<?php echo get_template_directory_uri(); ?>/img/logo.png" alt="" /> 

but how to make a dynamic image that is called by css

 .portfolio { background-attachment: fixed; background-image: url("../img/o-BUSINESS-TECHNOLOGY-facebook.jpg"); } 

I tried many times, but failed. Please suggest some ideas.

+5
source share
1 answer

It should be noted that Wordpress has a standard way of adding <script> and <style> to the <head> section of your web document, which uses the wp_enqueue_scripts action, but regarding your question, I will try to provide the desired way:

You can put this particular CSS in the <style> tags in your <head> documents and use:

 .portfolio { background-attachment: fixed; background-image: url("<?php echo get_template_directory_uri(); ?>/img/o-BUSINESS-TECHNOLOGY-facebook.jpg"); } 

UPDATE

Although this seems to work and gives you the desired result, I highly recommend that you not use it, because you can use:

 .portfolio { background-attachment: fixed; background-image: url("img/o-BUSINESS-TECHNOLOGY-facebook.jpg"); } 

in your style.css file and get the same result.

I really don't know what dynamic CSS you are after, but if you really want it to be dynamic and you are sure what you are doing, you have two methods:

  • The above method (adding styles to the <style> in the <head> section of the document)
  • creating a dynamic stylesheet using PHP (where you can get a tutorial from this link or google for a dynamic php style sheet )
+5
source

Source: https://habr.com/ru/post/1242032/


All Articles