• Hello,
    I have been trying really hard for long time, to have a variable sidebar content for my website.
    https://www.oole.eu

    Sidebar 1 is the one i want to change: introduce different images and links in the “Running Projects” & “Social Networks”

    Sidebar 2 i would like to keep it stable on all the pages.

    I think that the solution for this is to create multiple sidebar.php files and then introduce a conditional tag in my index.php template file.

    if (is_page1()) {

    then

    use sidebar1

    if (is_page2()) {

    then

    use sidebar2

    etc….

    Can somebody tell me if i am on the right track?
    Can you help me to draft this piece of code ? I have really tried hard to find a solution to this but is not easy for me.

    Thank you so much in advance.

Viewing 8 replies - 1 through 8 (of 8 total)
  • if (is_page(‘name of page’)) {
    get_sidebar(‘pagename’);
    }

    get_sidebar will get the sidebar-pagename.php file from your theme directory.

    So if you do get_sidebar(‘list’); it will get sidebar-list.php from your theme directory.

    Thread Starter easysun3000

    (@easysun3000)

    Hello Frumph, and thank you very much.

    I have done exactly what you suggested. Where name of the page is Page ID (actually a number).

    But unfortunatelly does not work. Is there maybe a specific place where i have to insert this piece of code in index.php?

    You can use the pagename..
    if(is_page('YOURPAGENAME')) { }

    or
    if(is_page( 8 )) { }

    When you use the ID (numeric) don’t include quotes around it, like in the first example…

    Thread Starter easysun3000

    (@easysun3000)

    Still, unfortunatelly does not work. Is there maybe a specific place where i have to insert this piece of code in index.php?
    Thanks.

    Perhaps it would be alot easier if you could show us your usage, eg. provide the code surrounding where you’re placing it, plus your placed code…

    Thread Starter easysun3000

    (@easysun3000)

    Hello, in the administration panel>appearance>editor, in the list of templates i have two sidebar templates:
    sidebar (sidebar.php)
    sidebar2.php (sidebar2.php)

    This is the code at the index.php (conditional sidebar is somewhere in the middle. Thank you !!!

    <?php get_header(); ?>
    <div id=”content”>

    <div id=”articles”>
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <div class=”article” id=”post-<?php the_ID(); ?>”>
    <div class=”articleMeta”>
    <div class=”dayMonth”><?php the_time(‘j M y’) ?></div>
    <div class=”comments”><?php comments_popup_link(‘Be first!’, ‘1 Com.’, ‘% Com.’); ?></div>
    </div><!– end articleMeta –>
    if (is_page(3)) {
    get_sidebar(‘sidebar2’);
    }
    <div class=”articleBody”>
    <h2>” rel=”bookmark” title=”Permanent Link to <?php the_title_attribute(); ?>”><?php the_title(); ?></h2>
    <?php the_content(‘ ‘); ?>
    <div class=”readMore”>#more” rel=”bookmark” title=”Read the entire article!”>Read the entire article!</div><div class=”comenteaza”><?php comments_popup_link(‘Post your comment’, ‘One comment’, ‘% comments’); ?></div>
    </div><!– end articleBody –>

    <div class=”clear”></div>
    </div><!–end article –>
    <?php wp_link_pages(); ?>
    <?php endwhile; ?>
    <div class=”navigation”>
    <div class=”alignleft”><?php next_posts_link(‘« Older articles’) ?></div>
    <div class=”alignright”><?php previous_posts_link(‘Newar articles »’) ?></div>
    <div class=”clear”></div>
    </div>
    <?php else : ?>
    <div class=”article”>
    <div class=”articleMeta”>
    <div class=”dayMonth”> </div>
    <div class=”category”> </div>
    <div class=”comments”> </div>
    </div><!– end articleMeta –>
    <div class=”articleBody”>
    <h2>Not Found</h2>
    <p>Sorry, but you are looking for something that isn’t here.</p>
    </div><!– end articleBody –>
    <div class=”clear”></div>
    </div><!–end article –>
    <?php endif; ?>

    </div>

    </div><!– end content –>

    <?php get_sidebar(); ?>

    <?php get_footer(); ?>

    why not use css ?

    Your conditions need to be enclosed in PHP tags, since they are PHP…

    if (is_page(3)) {
    get_sidebar('sidebar2');
    }

    Should be..

    <?php if (is_page(3)) {
    get_sidebar('sidebar2');
    } ?>

    You can see examples around the get_sidebar, get_footer and other areas that are PHP… otherwise your PHP code is taken as literal text/html…

    Anything inside of these are interpreted as PHP…
    <?php ?>
    Anything outside..
    <?php ?>I'm outside
    or
    </div>I'm still in HTML mode
    ..is read as HTML/text ..

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Variable Sidebar Content’ is closed to new replies.