Jump to content

What is the php code for customizing a website sidebar?


Hopeful
 Share

Recommended Posts

Hello, I am looking to customize by right sidebar for my WordPress website, and am wondering if there is a fairly easy way to do so with php code. I don't know all that much about code, but I have customized other codes on my site with good results. I am aware there is a custom sidebars plugin, but I'm trying to not depend upon plugins too much. And, I'm not sure if it has a way for me to do what I am looking to do or not either. There are certain pieces of my right sidebar that I am looking to have displayed on all of the pages of my site. This happens automatically by default. However, I am also looking for other pieces of my right sidebar to appear only on specific pages. Basically, I'm looking to have things that appear on every page and things that appear only on certain pages there simultaneously. In my sidebar page in the editor, I currently have the following code. It was there originally. It's just one part of the overall code that is there. Is there something I can put after this code to indicate I want "a" on only page "a" and "b" on only page "b" and "c" on every page, etc?Is there maybe a way to exclude something from appearing on certain pages by using page ID#'s or something? Then, if there were no excluded pages, whatever code I have would appear on every page?

<?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar('Sidebar_Right') ) : else : ?>

Does anyone know how this can be accomplished? And how I could tell the code to place things where I want them placed on the sidebar? Thanks in advance for your help!

Link to comment
Share on other sites

It sounds like the answers to your questions are probably inside the dynamic_sidebar function, wherever that gets defined. If you want to see a list of all of the files that Wordpress is using at any given point in the code, you can add this: print_r(get_included_files());

Link to comment
Share on other sites

Where would I add that? Sorry, I'm quite the newbie. I also found the following code in my functions editor. Maybe this gets altered in some way instead?

if ( function_exists('register_sidebar') )register_sidebar(array(  'name' => 'Sidebar_right',  'before_widget' => '<li id="%1$s" class="widget %2$s">',  'after_widget' => '</li>',  'before_title' => '<h2>',  'after_title' => '</h2>',

Link to comment
Share on other sites

Where would I add that?
At whatever point in the code you want to see the files that it's using. If it's calling a function like dynamic_sidebar, and you want to figure out where that function is defined, then add that code near where it calls the function to see the list of files that have been included at that point.
I also found the following code in my functions editor. Maybe this gets altered in some way instead?
There's no logic there, only data, but if you wanted to develop this as a plugin or something then you would probably add properties to the sidebar when you register it like that which would indicate which pages do or do not get that sidebar, and then the dynamic_sidebar function which presumably prints the markup for the sidebar would look at those properties to figure out if it should print it for the current page. Or, you could hard-code changes to the dynamic_sidebar function to only print certain sidebars in certain situations, which wouldn't really be a plugin since you would have to change that function instead of the sidebar setup to change which sidebars get shown where. It would be the easier way to go though, but less maintainable if you plan on making many changes in the future.
Link to comment
Share on other sites

Well, I can't say as though I completely understand everything you are saying, but through research, I think I may have found something that might work. But, maybe there's an easier way? And, I still have a question about it (which I'll ask below). I found this code in my Theme Functions editor:

));if ( function_exists('register_sidebar') )register_sidebar(array(  'name' => 'Sidebar_right',  'before_widget' => '<li id="%1$s" class="widget %2$s">',  'after_widget' => '</li>',  'before_title' => '<h2>',  'after_title' => '</h2>',));

What I did was copy all that one row down, and then changed 'Sidebar_right' to 'Sidebar_Right_2', which added a new place for me to put widgets in to. Then, I took another code I found through other research and applied to my sidebar editor area:

<div id="sidebar-center"><?php if (!is_page('1') && dynamic_sidebar('Sidebar_Right_2') ) : else : ?>	<?php endif; ?>  </div>  <div id="sidebar-center"><?php if (!is_page('2') && dynamic_sidebar('Sidebar_right') ) : else : ?>  <?php endif; ?>  </div>

If I just keep repeating that code, and input the page id's of all pages I want to exclude, and then put the widgets I want in to the appropriate places that I created with the first above code in my widgets area, then I think this will work (though it will be a lot of work to exclude all the pages for each one). My question with this is there are black dots appearing next to every item on the sidebar. Is there maybe a list code somewhere that's causing that? It looks like bullet points.

Edited by Hopeful
Link to comment
Share on other sites

Thank-you for the information. I am using a theme. Is there something in that above code itself that could be altered? Also, as much as I thought the above was basically my answer, it's unfortunately got a downfall for what I'm looking to do. Since that works off of excluding pages, then that means I would need to repeat a lot of the same page id#'s over and over again. As soon as I tested that out by putting page id's 1 and 2 in both places, then only page id 1 gets excluded, and nothing gets excluded on page id 2. Again, I'm a real newbie. I'm OK with copying and applying examples, but beyond that I don't yet know much. Thank-you everyone!

Link to comment
Share on other sites

Thank-you for the information. I am using a theme. Is there something in that above code itself that could be altered?
You could remove the li elements when you define the sidebar, but that may affect styling for the other elements if the CSS assumes the sidebar will be surrounded with li tags.
As soon as I tested that out by putting page id's 1 and 2 in both places, then only page id 1 gets excluded, and nothing gets excluded on page id 2.
Post your code, you may have not done it right.
Link to comment
Share on other sites

Thank-you for the li code information. I'll give that a try and see if that affects styling or not; hopefully not. And, I don't doubt that I've not done it right since I don't know enough about this yet.Here is the same code I used above, only altered as I mentioned a couple posts back. I wanted these pieces excluded from pg id's 1 and 2. When I duplicated the page id's here, it excluded both from only page 1, and did not exclude anything on page 2. In order for this to work for me, I would need it to somehow exclude a lot on those pages, and this would be duplicated for many pages of the site so that way I can drill down what pieces go with which pages as they will all be different.

div id="sidebar-center"><?php if (!is_page('1,2') && dynamic_sidebar('Sidebar_Right_2') ) : else : ?>	<?php endif; ?>  </div>  <div id="sidebar-center"><?php if (!is_page('1,2') && dynamic_sidebar('Sidebar_right') ) : else : ?>  <?php endif; ?>  </div>

Is there something about this code that has the potential to work, or do I need to try something else altogether?

Edited by Hopeful
Link to comment
Share on other sites

Thank-you for the suggestion, but I was unable to get that to work as well. That said, I have found what I believe to be the answer through the graciousness of a different source, and I'll share it here for anyone else who may be looking for this information. I needed to log in to my hosting company's CPanel, use the file manager, and drill down to my theme's .php files. I copied my sidebar.php file, and created a new one with the naming convention of sidebar-newname.php. It needs to be all lowercase and one word.This created a new .php file in my WordPress editor. Next, I went in to my functions.php file to create a new sidebar for me to drag widgets in to. I did this by inputting the following:

if ( function_exists('register_sidebar') )register_sidebar(array(  'name' => 'Sidebar_2',  'before_widget' => '<id="%1$s" class="widget %2$s">',  'after_widget' => '<br />',  'before_title' => '<h2>',  'after_title' => '</h2>',));

Sidebar_2 is what I named it here in this example, and you can replace that with whatever you want to name it.Please note that this code is a bit different than the above code, and it eliminates the 'li' parts, and therefore eliminates the bullet point issue I was experiencing before.The difference here is that I removed the 'li' before the id part, and then I replaced the after widget line with the br code to give each sidebar piece a space between them. After this, I went to my page.php file, and inputted the following:

<?php if (is_page('2')) : ?>   <?php get_sidebar('sidebar2'); ?>  <?php else : ?>	<?php get_sidebar(); ?>  <?php endif; ?><?php if (is_page('3')) : ?>   <?php get_sidebar('sidebar'3); ?>  <?php else : ?>	<?php get_sidebar(); ?>  <?php endif; ?></div><?php get_footer(); ?>

Please note that the code is repeated here.The sidebar2 is what I named my new .php file. I use only what comes after the sidebar- part. So, if you compare it to the above explanation, you would put newname there instead of sidebar2.The 2 is page id 2.What it seems to be saying is that whatever is in sidebar2, put it on page id 2, or if there's nothing in sidebar2, then get whatever is in the sidebar.php file. I also needed to open my newly created sidebar2 php file, and edit one piece of code like so:

	<?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar('Sidebar_2') ) : else : ?>

The only part I changed there was the Sidebar_2 part. Next, I just needed to drag what I wanted for widgets in to the appropriate sidebars. I have done some preliminary testing with this, and it seems to be working in just the way I've been looking for it to work. I hope that makes sense, and thanks everyone for your time!

Edited by Hopeful
Link to comment
Share on other sites

If it's easier to understand the code, you can replace this:

<?php if (is_page('2')) : ?><?php get_sidebar('sidebar2'); ?><?php else : ?><?php get_sidebar(); ?><?php endif; ?><?php if (is_page('3')) : ?><?php get_sidebar('sidebar'3); ?><?php else : ?><?php get_sidebar(); ?><?php endif; ?>

with this:

<?phpif (is_page('2'))  get_sidebar('sidebar2');else  get_sidebar(); if (is_page('3')) :  get_sidebar('sidebar'3);else  get_sidebar();?>

or, even shorter:

<?phpis_page('2') ? get_sidebar('sidebar2') : get_sidebar();is_page('3') ? get_sidebar('sidebar3') : get_sidebar();?>

It would probably be more correct to do this though:

<?phpif (is_page('2'))  get_sidebar('sidebar2');elseif (is_page('3'))  get_sidebar('sidebar3');else  get_sidebar();?>

You can even use a switch statement:

<?phpswitch (true){  case is_page('2'): get_sidebar('sidebar2'); break;  case is_page('3'): get_sidebar('sidebar3'); break;  default: get_sidebar();}?>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...