• Resolved xceller8

    (@xceller8)


    Hello,

    I’ve been exploring ways to have different backgrounds for different Pages in the Sharpfolio theme I am currently using. With my very limited knowledge with CSS, I managed to modify the background in the “Style.css” file but it makes it the default background for everything. While this is ok, I’d like to try having different backgrounds for my About, Archives etc. sections. I know theres the Template option that I can assign for Pages, but once again limited knowledge with css and I read a bunch of tutorials leaving me scratching my head. Does anyone know how to do this?

    Here is my work-in-progress page I am working on:
    https://www.ray-ray.ca

    And here’s the line of code I changed for the background in my style.css file:

    body {
    	text-align: center;
    	background: #191919 url('images/bg1d4.jpg') repeat-x top; width: 100% height: 100%
    	color: #FFFFFF;
    	font-family: 'Lucida Grande', Helvetica, Arial, sans-serif;
    	font-size: 11px;
    	line-height: 24px;
    }

    Thanks for reading this and the help. Cheers.

Viewing 15 replies - 1 through 15 (of 18 total)
  • Thread Starter xceller8

    (@xceller8)

    Thanks for the comments alchymyth and t31os_

    I reviewed the two sites and well once again limited knowledge = me confused. I did some further digging and I found this site which I thinks is pretty much what I need different templates having different backgrounds.

    https://fearlessflyer.com/2009/05/how-to-change-the-background-dynamically-in-your-wordpress-theme/

    The only problem I’m having, and currently testing out, is modifying my style.css file. In his “How To…” it says modify the body ideas to match what is posted in the header.php. Lets say I kept all the example names the same way and it is the same as what is coded in the header.php would the change I made work?

    Below is how the current style.css is written:

    body {
    	text-align: center;
    	background: #191919 url('images/bg1d4.jpg') repeat-x top; width: 100% height: 100%
    	color: #FFFFFF;
    	font-family: 'Lucida Grande', Helvetica, Arial, sans-serif;
    	font-size: 11px;
    	line-height: 24px;
    }

    And his tutorial outlines to change the body as:

    background:url(images/default-bg.jpg) top center no-repeat #000000; }
    body#template-diff-bg-demo1 { background:url(images/default-bg2.jpg) top center no-repeat #000000;}
    body#template-diff-bg-demo2 { background:url(images/default-bg3.jpg) top center no-repeat #000000; }

    So would their be any problem if I were to modify the original code to do this:

    body {
    	text-align: center;
             background:url(images/default-bg.jpg) top center no-repeat   #191919; }
             body#template-diff-bg-demo1 { background:url(images/default-         bg2.jpg) top center no-repeat #191919;}
             body#template-diff-bg-demo2 { background:url(images/default-bg3.jpg) top center no-repeat #191919; }
    	color: #FFFFFF;
    	font-family: 'Lucida Grande', Helvetica, Arial, sans-serif;
    	font-size: 11px;
    	line-height: 24px;
    }

    ?

    I’m confused because how this code was written is different what I am seeing in these help pages. Thanks again for your time and the help given.

    Thread Starter xceller8

    (@xceller8)

    Ok I tried it and I think I need to change something because now everything that was in the center of the page is now in the left and right

    https://www.ray-ray.ca

    S.O.S

    first step: restore your style.css, i.e. the body style, to what it was before:

    body {
    	text-align: center;
            background:url(images/bg1d4.jpg) top center no-repeat #191919;
    	color: #FFFFFF;
    	font-family: 'Lucida Grande', Helvetica, Arial, sans-serif;
    	font-size: 11px;
    	line-height: 24px;
    }

    you could then post the code from header.php near the body tag:
    anything from </head> to <div id="wrapper"> to show how the body tag is used in your theme.

    at the moment it shows as

    <body id="
    default" >

    which looks like as if there is some php code involved.

    Thread Starter xceller8

    (@xceller8)

    Hi alchymyth, thanks for getting back to me. I definitely restored the style.css code back to its original state, after seeing its result.

    As instructed in the tutorial, I changed my header.php code to be:

    <body id="
    <?php if (is_page_template('template-diff-bg-demo1.php')){
           echo "template-diff-bg-demo1";}
    elseif (is_page_template('template-diff-bg-demo2.php')){
           echo "template-diff-bg-demo2";
    }else{
    echo "default";
    }?>" >
    
    <div id="wrapper">

    According to your suggestion I could input that code into the style.css so that it reads:

    body {
    text-align: center;
    background: #191919 url(‘images/bg1d4.jpg’) repeat-x top;
    color: #FFFFFF;
    font-family: ‘Lucida Grande’, Helvetica, Arial, sans-serif;
    font-size: 11px;
    line-height: 24px;
    }

    <body id=”
    <?php if (is_page_template(‘template-diff-bg-demo1.php’)){
    echo “template-diff-bg-demo1”;}
    elseif (is_page_template(‘template-diff-bg-demo2.php’)){
    echo “template-diff-bg-demo2”;
    }else{
    echo “default”;
    }?>” >

    div#wrapper {
    text-align: left;
    margin: auto;
    width: 800px;
    }`

    And this should work? My apologies if I misunderstand, I’m still trying to get a handle on the language.

    Thread Starter xceller8

    (@xceller8)

    Sorry here it is in the code box

    body {
    text-align: center;
    background: #191919 url('images/bg1d4.jpg') repeat-x top;
    color: #FFFFFF;
    font-family: 'Lucida Grande', Helvetica, Arial, sans-serif;
    font-size: 11px;
    line-height: 24px;
    }
    
    <body id="
    <?php if (is_page_template('template-diff-bg-demo1.php')){
    echo "template-diff-bg-demo1";}
    elseif (is_page_template('template-diff-bg-demo2.php')){
    echo "template-diff-bg-demo2";
    }else{
    echo "default";
    }?>" >
    
    div#wrapper {
    text-align: left;
    margin: auto;
    width: 800px;
    }

    if you have these template files: ‘template-diff-bg-demo1.php’ and the other, and want a special body background for pages using these template files,

    then your styles could look like:

    body {
    text-align: center;
    background: #191919 url('images/bg1d4.jpg') repeat-x top;
    color: #FFFFFF;
    font-family: 'Lucida Grande', Helvetica, Arial, sans-serif;
    font-size: 11px;
    line-height: 24px;
    }
    
    body#template-diff-bg-demo1 {
    background: #191919 url(images/default-bg2.jpg) top center no-repeat;
    }
    
    body#template-diff-bg-demo2 {
    background: #191919 url(images/default-bg3.jpg) top center no-repeat;
    }
    Thread Starter xceller8

    (@xceller8)

    Thanks alchymyth, I’ll give it a try, but I don’t have ‘template-diff-bg-demo1.php’ of course so I’ll have to work on that part now, but once I do I hope it works.

    why would you make a new page template, just to be able to show a different background on different pages?

    coming back to the ‘body_class()’ – which does not yet have a good instruction page in the codex – but –

    if you replace:

    <body id="
    <?php if (is_page_template('template-diff-bg-demo1.php')){
           echo "template-diff-bg-demo1";}
    elseif (is_page_template('template-diff-bg-demo2.php')){
           echo "template-diff-bg-demo2";
    }else{
    echo "default";
    }?>" >

    with:
    <body <?php body_class(); ?> >

    then you can run your site and click on the page(s) where you would like to have a different background; then use the ‘view source’of your browser, and find the <body class="a lot of special classes" > in the html code it shows you, where there should be a class in the list that is unique to your page.

    maybe this article helps to explain it a bit better https://www.nathanrice.net/blog/wordpress-2-8-and-the-body_class-function/

    Thread Starter xceller8

    (@xceller8)

    Hello,

    So I’ve been trying to figure this out with the tutorial sent to me:
    https://www.nathanrice.net/blog/wordpress-2-8-and-the-body_class-function/

    Since my method didn’t work. Initially I thought that by creating the .php files as quoted in the previous post for example creating the “template-diff-bg-demo1.php” file and writing to code:

    body {
    text-align: center;
    background: #191919 url('images/bg1d4.jpg') repeat-x top;
    color: #FFFFFF;
    font-family: 'Lucida Grande', Helvetica, Arial, sans-serif;
    font-size: 11px;
    line-height: 24px;
    }
    
    <body id="
    <?php if (is_page_template('template-diff-bg-demo1.php')){
    echo "template-diff-bg-demo1";}
    elseif (is_page_template('template-diff-bg-demo2.php')){
    echo "template-diff-bg-demo2";
    }else{
    echo "default";
    }?>" >
    
    div#wrapper {
    text-align: left;
    margin: auto;
    width: 800px;
    }

    in the style.css and in the header.php:

    <body id="
    <?php if (is_page_template('template-diff-bg-demo1.php')){
           echo "template-diff-bg-demo1";}
    elseif (is_page_template('template-diff-bg-demo2.php')){
           echo "template-diff-bg-demo2";
    }else{
    echo "default";
    }?>" >
    
    <div id="wrapper">

    And by simply setting the template for the page to the desired php it would give that page the desired background image. This didn’t work and I don’t know why so I tried my interpretation of alchymyth’s solution which is in the header.php I replaced:

    <body id="
    <?php if (is_page_template('template-diff-bg-demo1.php')){
           echo "template-diff-bg-demo1";}
    elseif (is_page_template('template-diff-bg-demo2.php')){
           echo "template-diff-bg-demo2";
    }else{
    echo "default";
    }?>" >

    with…

    <body <?php body_class(); ?> >

    I tried this for the About section of my page and found the body class code to be:

    <body class="page page-id-2 page-template page-template-about-php logged-in" >

    So I took the “page page-id-2 page-template page-template-about-php logged-in” and revised the code in the style.css to read:

    body {
    text-align: center;
    background: #191919 url('images/bg1d4.jpg') repeat-x top;
    color: #FFFFFF;
    font-family: 'Lucida Grande', Helvetica, Arial, sans-serif;
    font-size: 11px;
    line-height: 24px;
    }
    
    body#page page-id-2 page-template page-template-about-php logged-in {
    background: #191919 url(images/bg1d.jpg) top center no-repeat;
    }
    
    body#page page-id-254 page-template page-template-default logged-in {
    background: #191919 url(images/bg1d3.jpg) top center no-repeat;
    }

    This didn’t work and now I’m lost and not sure what to do. I’m reading up on Adding Classes in Nathan Rice’s tutorial and looked into conditions but with my limited knowledge in coding its like trying to fit a large square into a small circle hole and this is not Apollo 13.

    If someone can help that would be great and appreciated. Thank you for reading my story.

    you are nearly there:
    you don’t need to take all the body classes that the function adds to the body tag – the essential and unique ones are .page-id-2 and .page-id-254
    also, classes are denoted by a . infront of the name; while the # would denote an id – different things in css.

    so, with the essential body classes and a dot . before the class name, your styles would look like:

    body {
    text-align: center;
    background: #191919 url('images/bg1d4.jpg') repeat-x top;
    color: #FFFFFF;
    font-family: 'Lucida Grande', Helvetica, Arial, sans-serif;
    font-size: 11px;
    line-height: 24px;
    }
    
    body.page-id-2 {
    background: #191919 url(images/bg1d.jpg) top center no-repeat;
    }
    
    body.page-id-254 {
    background: #191919 url(images/bg1d3.jpg) top center no-repeat;
    }

    hope this works, good luck ??

    Thread Starter xceller8

    (@xceller8)

    EUREKA!

    It worked! Thank you alchymyth! It feels like an apple fell on my head and it worked. I would say that the apple was my idea and it just hit me but I couldn’t of done it without your help. Thank you again! Really appreciated!

    Hey guys, this has been really helpful. However I’m coming up with one problem. When I put the code <body <?php body_class(); ?> > into my header it’s fine on the “pages” but when I navigate to the blog or blog category pages the whole body shifts to the left (the alignment or float becomes left).

    Any suggestions. It’s defiantly this code as when I’ve removed it it works fine.

    Thanks.

    probably a interference of some theme css classes with one of the automatic generated body_classes.
    (theme authors are likely to use css classes such as .page, .post, .blog etc.)
    for instance, just today there was a thread here where .search of the search result page would interfere with a class of a search form.

    but that is usually easy to fix.

    more help in exchange with a link to the live site with the problem showing.

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘Modifying to have different backgrounds for different posts’ is closed to new replies.