D
D
dm2015-06-27 05:40:05
PHP
dm, 2015-06-27 05:40:05

CSS inside PHP, how to do it right?

Site on WordPress.
On the main page, you need to add styles for the header. It is impossible in .css, because the header is on all pages, and you only need to change it on the main one.
Did it like this:

<?php if( is_home() ) { ?>
<style>		   	
// ...
</style>
<?php } ?>

It works, but, firstly, styles are obtained in html, and secondly, it looks somehow clumsy. Is there another way to style only the main page? Maybe using JS somehow.
PS I did this:
<body <?php if( is_home() ) body_class('homepage') ?>>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
F
faragly, 2015-06-27
@Chekhoved

I suggest not to spoil the html with styles, just add a class or id to the body:

<body<?if(is_home()):?> id="homePage"<?endif;?>>
...
</body>

and in the styles for the home page, create styles that start with #homePage.

A
Andrew, 2015-06-27
@Ashlst

Move the styles to a separate file. Add a check for the page id, if this is the main one, we include the file, no, we don’t)

X
xmoonlight, 2015-06-27
@xmoonlight

Router, I hope they did it to check the URI?
This is how we act!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question