Mentalhead
Mentalhead

Reputation: 1505

Changing CSS with PHP DOM

I'm making a Wordpress website, and I have a question. Can I change CSS of certain elements with PHP DOM? I don't want to change structure of HTML, just few styles if certain conditions are met.

For example, if no images are present in my post I would like to add change color of the links in that post.

I'm thinking doing that with jQuery since it sounds a lot simpler, but I'm just wondering is this a valid way to do, or should I use PHP DOM?

Upvotes: 0

Views: 211

Answers (3)

Marcos Vives Del Sol
Marcos Vives Del Sol

Reputation: 479

You can easily create an inline CSS block using PHP without PHP-DOM:

<?php if ($linkCount == 0) { ?>
    <style>
        a:link {
            color: red;
        }
    </style>
<?php } ?>

Upvotes: 0

Zmart
Zmart

Reputation: 1193

For this type of operation, it is generally advised to do it with jQuery. This will save you processing time and power on your server for something easily done by the browser.

If, however, it absolutely must be done server-side, then you can achieve this with DOMAttr:

$attr = $element->setAttributeNode(new DOMAttr('style', 'border:1px solid black;'));

Upvotes: 1

Daniele
Daniele

Reputation: 1938

You can try with something like this:

if(!$('img').length){
    $('a').css('color', 'red');
}

here a jsfiddle with the example

Upvotes: 0

Related Questions