Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Nov 2011
    Posts
    136
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Question PHP if/then statement to call specific CSS?

    Hi there,
    I've got a WordPress site I'm building, and I've got things set up so that the PHP knows not to display the sidebar on a Page, but will display it on a Post. Unfortunately, the width of the main text area is universally set up in the CSS to be right for when there's a sidebar. Is there a way to create some sort of an if/then statement that calls an alternate piece of CSS for the Page without the sidebar so my content will stretch the full width in that case?

    Thanks in advance.

    Please note that I am no expert in PHP. I can move things around and hopefully not break stuff, but I couldn't write a call from scratch to save my life right now.

    ~Laura

  • #2
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    331
    Thanks
    2
    Thanked 47 Times in 46 Posts
    Yeah. You can. Something like. Or use a Switch Statement.

    A switch statement is probably better.

    PHP Code:
    <?php
    //Yourfile.php

    if()//condition
    {
         
    $yourcssfile 'css1.css';   
    }
    elseif() 
    //2nd condition
    {
         
    $yourcssfile 'css2.css'
    }
    ?>

    <html>
    <head>
    <link rel="stylesheet" type="text/css" href="<?=$yourcssfile?>">

    </head>
    </html>

  • #3
    Regular Coder Custard7A's Avatar
    Join Date
    Jul 2010
    Location
    Australia
    Posts
    286
    Thanks
    32
    Thanked 33 Times in 33 Posts
    Building on what Redcoder said, the condition you use is likely related to what you've done with PHP to only display the sidebar on some pages. Also, since you probably have a lot of CSS that you don't want to change or copy into a new file just to make it fit there being no sidebar, you could leave the current CSS, and just add a new CSS file in that supersedes the few select things you want by means of the cascade. If the condition is met that is.

    PHP Code:


    <html>
    <head>
    <link rel="stylesheet" type="text/css" href="original.css">

    <?php

      
    if(no sidebar) {

      print 
    '<link rel="stylesheet" type="text/css" href="overwriting.css">';

      } 
    ?>

    </head>
    </html>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •