Jon Imms
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
}
?>
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.
I want it so that if you are on page 6124, i want this extra stylesheet to be used /css/aortic-heart-standard
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_dire ctory() . "/css/aortic-heart-standar d.css'>";
} else if (is_page(20)) {
echo "<link rel='stylesheet' type='text/css' href='.get_stylesheet_dire ctory() . "/css/aortic-heart-atomic. css'>";
} else {
// do something else
}
?>
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_dire
} else if (is_page(20)) {
echo "<link rel='stylesheet' type='text/css' href='.get_stylesheet_dire
} else {
// do something else
}
?>
ASKER
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
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'>
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
}
?>
Of course it is. You forgot to close the " before the .
Dan
<?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
}
?>
HTH,Dan
ASKER
almost there :)
It is printing
This takes you to a 404 error page. Iwant it to print
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'>
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'>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Is the ELSE condition occurring every time?
https://codex.wordpress.org/Function_Reference/is_page
Also, get_stylesheet_directory()
Open in new window