Support » Developing with WordPress » Sidebar Per Page

Viewing 15 replies - 1 through 15 (of 22 total)
  • if ( is_page('archive') ) get_sidebar('archive');
    if ( is_page('links') ) get_sidebar('links');

    have a sidebar-archive.php, sidebar-links.php file in your theme directory.

    This is just an example.

    You still have to register the sidebar’s in your functions.php and inside those sidebar.php files

    <?php if ( !function_exists(‘dynamic_sidebar’) || !dynamic_sidebar(‘Links’) ) : ?><?php endif; ?>

    or !dynamic_sidebar(‘Archive’) .. you get the idea…

    inside your functions.php

    if ( function_exists(‘register_sidebar’) ) {

    That’s one way, or if you just call the custom sidebar from within the template being used, then you don’t need to register anything in your functions…

    E.g. on archive.php, at the bottom where your sidebar call is, use:

    ~<?php get_sidebar(‘archive’); ?>~

    It may mean a few extra template files instead of just allowing WP to use the index.php file (I use archive.php, archives.php, categories.php, etc) but then you don’t have to remember to modify your functions every time you add a new custom sidebar.

    Or, use ONE sidebar and conditional logic to display what you need for each view…? One file means one place to edit – that’s what I like about using a single file.

    Note, too, per Frumph’s advice, that you *can* make an array of your logic

    if (is_page('a') || is_page('b') || is_page('c')) {//do stuff here}

    using additional elseif, else statments as necessary. Similar logic for category arrays can be found in the documentation…it’s not the same as for pages.

    The conditional logic concept for a sidebar is great way to go about doing something like that. And it’s easy to manage, since you have one file called sidebar.php

    Here’s an example of something I used:

    <?php  if ( is_page('Section 1') || $post->post_parent == '1' ) { ?>
    	 <p> Custom content for "Section 1"</p>
      <?php } elseif ( is_page('Section 2') || $post->post_parent == '2') ) { ?>
    	 <p> Custom content for "Section 2"</p>
    <?php } else { ?>
    	 <p> Generic sidebar content</p>
    <?php } ?>

    It checks the page name to see if it’s the parent page for a section, or if it’s a child of that parent page. I used it like this because I have custom content I wanted to display for different sections of my website.

    If none of the statements match, it defaults to a generic set of sidebar content.

    i am basically trying to do the same thing as a test for an upcoming project, but was testing using the footer.php.

    i basically duplicated the footer.php file and called it test.php

    then i went to the page.php and added this at the bottom

    <?php get_test(); ?>

    not working yet, so what else do I need to do to make the test.php content show on the page.php pages?
    do I need to add or edit the functions.php? (or anything else)

    sorry for hijacking this thread.

    I am going just about nuts.
    I am making a custom theme. I have created two sidebars one will be for the front page and the other for the blog itself.
    I have created a function:

    if ( function_exists('register_sidebar') ){
    	'name' => 'blog',
    		'before_widget' => '<div class="sidebar-item">',
    	    	'after_widget' => '</div>',
    				'before_title' => '<h2>',
    					'after_title' => '</h2>', ));
    	'name' => 'front',
    		'before_widget' => '<div class="sidebar-item">',
    	    	'after_widget' => '</div>',
    				'before_title' => '<h2>',
    					'after_title' => '</h2>', ));

    Inside the sidebars I added their names:

    <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('blog') ) : ?>
    <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('front') ) : ?>

    and i added the names in the index.php and the front.php (respectively). The sidebars do show up in the widget settings and on the site. However the only thing I can see is the default sidebar(s) settings. As if I didn’t add any widgets.
    Seriously it’s almost two in the morning and I’ve been working for the last twelve hours non-stop. This is the only thing that is killing my work.
    Please tell me what I am doing wrong.

    Moderator Ipstenu (Mika Epstein)


    🏳️‍🌈 Halfelf Rogue & Plugin Review Team Rep

    Why not do a normal widgitized sidebar and then use Widget Logic to handle it?

    thanks , but I ended up fixing the issue. I wanted to be able to use the code rather than a plugin. That one does look interesting though.

    Keep in mind that if your sidebar(s) is/are not registered in functions.php you can’t use widgets.

    Hi all, I am new to wordpress and am experimenting with the “Hamasaki” theme, it comes with 4 ads in the sidebar which I made into 6—also, other than the Home page(index) I have 5 Parent categories. I do not use google adsense or anything like that, my question, is it possible to have display different ads in the different categories? now the ones on the homepage display throughout the page. I tried to work with the sidebar.php but cannot seem to nail it. My ads will consist of a jpg in my images folder with a link to the advertisers site. Would appreciate any help I can get.
    Thank you

    Sure it’s possible. You can use Conditional Tags so that on a certain page a given variation of the sidebar is displayed.

    Thank you SS_Minnow, let me see if I am new to wordpress, let me see if I can figure out the Conditional Tags

    If you wanted to have a different sidebar on a page called “Blog” than what you have on your other pages, you would put this into the page.php file where it calls in the sidebar:

    <?php if ( is_page('blog') ) { ?>
    <?php include(TEMPLATEPATH.'/sidebar2.php');?>
    <?php } else { ?>
    <?php include(TEMPLATEPATH.'/sidebar.php');?>
    <?php } ?>

    If the sidebar is registered in functions.php the code can be done differently but you can find that code by searching the forum & codex.

    I share the same problem, I want to have different sidebars to different pages? from what I can understand the problem has been solved. The things is that programming is not one of my strenghts, so I would like it if someone explained it to me as if you were explaining it to an infant 😉

Viewing 15 replies - 1 through 15 (of 22 total)
  • The topic ‘Sidebar Per Page’ is closed to new replies.