Link to home
Start Free TrialLog in
Avatar of Jon Imms
Jon ImmsFlag for United States of America

asked on

If Else statement Wordpress Childtheme

I am trying to get an if/else statement working in my header, which will use a staylsheet depending on which page you are on.   I am using a child theme, and added a new directory called css, which has a few css files inside.  How do i correctly do a if else statement.  Below is my failed attempt :(


<?php  if(is_page(6124)) { 
    echo "get_stylesheet_directory() . '/css/aortic-heart-standard.css')";
  } else if (is_page(20)) {
   echo "get_stylesheet_directory() . '/css/aortic-heart-atomic.css')";
  } else { 
    // do something else
    }
 ?>

Open in new window

Avatar of Matthew Kelly
Matthew Kelly
Flag of United States of America image

Ok, actually I believe the issue is with the is_page and not the if/else statement.

Is the ELSE condition occurring every time?

https://codex.wordpress.org/Function_Reference/is_page

Also, get_stylesheet_directory() is a PHP function so for it to execute, your echo would need to be the following. I am also assuming you need the "')" after .css, but am unsure.

<?php  if(is_page(6124)) { 
    echo get_stylesheet_directory() . "/css/aortic-heart-standard.css')";
  } else if (is_page(20)) {
   echo get_stylesheet_directory() . "/css/aortic-heart-atomic.css')";
  } else { 
    // do something else
    }
 ?>

Open in new window

Avatar of Jon Imms

ASKER

It's not working.  It is printing the filepath of the file in the header.

I want it so that if you are on page 6124, i want this extra stylesheet to be used /css/aortic-heart-standard.css

and so on.  

Whichever way i try playign with it, it either just prints it at the top of my page, or does not include it at all.
What exactly do you mean by "It's not working"?

When you say it is printing the file path, that leads me to believe you are not putting the rest of the required css html link around it like below?

<?php  if(is_page(6124)) {
    echo "<link rel='stylesheet' type='text/css' href='.get_stylesheet_directory() . "/css/aortic-heart-standard.css'>";
  } else if (is_page(20)) {
   echo "<link rel='stylesheet' type='text/css' href='.get_stylesheet_directory() . "/css/aortic-heart-atomic.css'>";
  } else {
    // do something else
    }
 ?>
ok, i changed to the above code (i was forgetting to wrap the stylesheet tags around)

It is printing this when i look at the source code, so not picking up the stylesheet

<link rel='stylesheet' type='text/css' href='.get_stylesheet_directory() . '/css/aortic-heart-standard.css'> 

Open in new window

Sorry, missing " before .get_
<?php  if(is_page(6124)) { 
    echo "<link rel='stylesheet' type='text/css' href='".get_stylesheet_directory() . "/css/aortic-heart-standard.css'>";
  } else if (is_page(20)) {
   echo "<link rel='stylesheet' type='text/css' href='".get_stylesheet_directory() . "/css/aortic-heart-atomic.css'>";
  } else { 
    // do something else
    }
 ?>

Open in new window

Avatar of Dan Craciun
Of course it is. You forgot to close the " before the .
<?php  if(is_page(6124)) { 
    echo "<link rel='stylesheet' type='text/css' href='".get_stylesheet_directory() ."/css/aortic-heart-standard.css'>";
  } else if (is_page(20)) {
   echo "<link rel='stylesheet' type='text/css' href='".get_stylesheet_directory() ."/css/aortic-heart-atomic.css'>";
  } else { 
    // do something else
    }
 ?>

Open in new window

HTH,
Dan
almost there :)

It is printing
<link rel='stylesheet' type='text/css' href='/home/dashash/public_html/onxlti/wp/wp-content/themes/metric/css/aortic-heart-standard.css'> 

Open in new window


This takes you to a 404 error page.  Iwant it to print

<link rel='stylesheet' type='text/css' href='/wp/wp-content/themes/metric/css/aortic-heart-standard.css'> 

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Matthew Kelly
Matthew Kelly
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial