0

For my new wordpress site I need some help. I got 4 pages and 2 different sidebars. On 2 pages there shouldn't be a border-left and on the other 2 pages there should be a border-left. So I got following code:

<div id="sidebar" class="sidebar">
        <?php if(is_page('Willkommen') || is_page('Angebot'))
            {
                dynamic_sidebar('Angebot');
            }

            else
            {
                dynamic_sidebar('Anfahrt');
                if(is_page('Anfahrt'))
                {
                    echo "<script type='text/javascript'>
function removeBorder()
{
    $(document).ready(function() {
        if($('#sidebar').hasClass('sidebar'))
        {
            $(this).removeClass('sidebar')
            $(this).addClass('secondsidebar')
        }

    });
}
</script>";
                }
            }
        ?>
    </div><!-- sidebar -->

I want to remove the class "sidebar" and add the class "secondsidebar" if there is a div#sidebar on the page. But the code won't work. I wrote this code for myself and I'm a beginner in jQuery :) So please be patient.

I hope someone can give me a hint.

Cheers

1
  • You define a function removeBorder here, but never call it. Commented Sep 7, 2013 at 21:07

1 Answer 1

2

A pure PHP solution should be possible as well.

<div id="sidebar" class="<?php if (is_page('Anfahrt')) : ?>secondsidebar<?php else : ?>sidebar<?php endif; ?>">
    <?php
    if (is_page('Willkommen') || is_page('Angebot')) {
        dynamic_sidebar('Angebot');
    }

    else {
        dynamic_sidebar('Anfahrt');
    }
    ?>
</div>

NOTE: try not to use IDs and classes with the same name. #sidebar and .sidebar might get confusing.

Explanation for the classes:

<div id="sidebar" class="<?php if (is_page('Anfahrt')) : ?>secondsidebar<?php else : ?>sidebar<?php endif; ?>">

When the server reads this (PHP is code that is executed by the server before rendering the result back to the user) it comes to "class" and it notices an if-statement. "what text should I put in the class="" of this element?" The server then sees the instructions:

  • If the page is "Anfahrt": the text to return should be secondsidebar
  • Else (in all other cases): the text should be sidebar

"Text" isn't really a good name here. But you get what I mean. This "text" is then placed where the if-statement is. The result then is (as it is returned to a user):

If the page is "Anfahrt":

<div id="sidebar" class="secondsidebar">

If the page is anything else:

<div id="sidebar" class="sidebar">
Sign up to request clarification or add additional context in comments.

6 Comments

Hm okay. Yeah it make sense. But it still doesn't replace the class "sidebar" with "secondsidebar"
Yeah I looked at your edit and I will thank you for explaining this so nice, but it still doesn't work. If I visit page "Anfahrt" it doesn't replace the class "sidebar" with "secondsidebar" :/ Is still something wrong?
@Sergio Look closely, it isn't inside the function anymore. The first part was, the result doesn't.
Aaaaaah nice! It works now! Thank you. But: Can you explain it a little bit closer to me? Because I don't want to have a code that I don't understand :)
Ah yeah that sounds logical :) But for someone who has never looked closer to PHP it isn't as simple as it seems to write something like this :) Thank you for your help and support and for the very nice explanation :) (Y)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.