Is_page() conditional logic

Hi,
I’m trying to write some conditional logic to have Wordpress apply a particular custom style to a particular page.
I can get the custom style ‘police-lights’ to run on all pages, by adding it to the div below (from header.php)

<div id="content" class="site-content police-lights">

but I just want it to take effect on one particular page not all pages.
I’m pretty sure I need to use is_page() with the post ID inside the brackets but not sure how to write the if statement or where to place the if code block.

Any help appreciated.

Hi @Argent, maybe the following code will work for you, but please note I have not tested it:

<?php
$classes = array("site-content");
if ( is_page("myPage") ) $classes[]="myClass";
?>
<div id="content" class="<?php echo join(' ', $classes) ?>">

Hope it helps.

2 Likes

Andres,
Thanks for that, I found this and it’s working now. I appreciate your help.

	<?php  if ( is_page('2942') ) { ?>
		<div id="content" class="police-lights">
	<?php } else { ?>
		<div id="content" class="site-content"> <?php } ?> 

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.