Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Sidebars are most often used to provide links and further information about the site to your visitors. This is normally done through widgets, but in some cases you may want to add some permanent static content to the sidebar as well. There are many reasons for this, but one example is if you want to add social media links at the top of the sidebar permanently. As you can see, in my site right now I have some links to social media here. They're just regular text links and they're added in through a widget. So I can go into my Widgets and show you the actual links.
They're right here, under Main Sidebar in the Text widget, and here is some standard HTML that creates those links. What I want to do is put this code into my sidebar.php template instead so it's there permanently, no matter what the widgets do, and I also want to restyle it so that it looks a bit nicer, with some icons instead of just text. The first thing I need to do is get the sidebar.php file over in my child theme, so I'll go and find sidebar.php and copy it over and then I can make changes to it.
Now, this gets a little bit tricky because I know that when you add a widget in, a lot of the styling that's coming into the widget is actually coming from the functions file. Now, before we start pasting content directly in here, it's important to understand how the widgets are inserted into the page. If you look at the function that creates a widget in the functions file--I can go to the functions.php in the parent theme and go to register widgets--you see that the function inserts code before and after the widget, and also defines code before and after the title.
So it's really important that we match this code when we add new code into our sidebar.php file. You see the new widget starts with an aside that has the ID of the widget and then the class of widget, and then it also has an h3 with the class of widget-title. So we have to duplicate this where we want to create our widget. So I'll start off by inserting a new side directly above where the sidebar is going to appear. I'll give it the ID social-icons, and I'll also give it the class widget and clear.
I'll explain the clear part in a second. Then I'll close my aside and then go back to functions and take a look, just to make sure I have the same stuff, and then I also need this h3 class widget title. so I'll put that in, and I will say, Connect with us! So that's going to be the title of my widget. Now I can copy in the code from my original widget. It's right here, so I'll copy all that in, paste it in, and then I just need to do some quick cleanup here, because I like all my code to be nice and clean.
And now when I save this in the new sidebar.php file and I open my site I see I now have Connect with us! with these links, and then I have the widget directly underneath. So now I can get rid of my widget, so I'll go and remove the widget. I'll dump it down here in inactive widgets. And when I reload the page again, you see we now have the widget we want up here, but it's permanent.
Now I want to style this code so that it looks the way I want it to. Now before I started this I went online and I found some social media icons I want to use. They're here. Now I want to place these inside my child theme so that I can use them to style. So I'll go to my child theme folder and I'll create a new folder called images and then place these four icons inside that folder. Now I can go to my style sheet and call them.
So first I have to find a logical place to put my code. I'd say it's right here before pagination. And now I need to style that content that I put in. If you look at the code I created, you'll see that I actually added in a lot of extra code so that it's easier to style, and I'll explain what I did here. We start off with an aside that has the id social-icons. That's just so we can know what all this content is. Then you see that each of the items is actually a link that is inside a list item, and each of the list items has a class that defines what that link is: in this case social-twitter, social- facebook, social-flickr, and social-contact.
I do this so that I can associate the correct image with the link without putting the image into the link itself. In other words, I'll use CSS to replace the text here--Follow Red 30 on Twitter-- with an image or the Twitter logo. This requires a fair bit of explanation, so to make it a little bit by quicker I've created code snippets for you that you can use. If you go to the codesnippets file and you go to chapter 5.7, you'll find all the code I already pasted in, as well as all the style sheet.
So you can copy out the code here and paste it into your sidebar.php file in your child theme and then you can copy out the style code below and paste it into your child theme style sheet. Now that I have the code, I can explain to you what happens here. First, I define all the links that are found under the social-icons ID to be displayed as a block, to float to the left, to have a width and height of 32 pixels, and to have a margin on the right and the bottom of 10 pixels.
I also indent the text by 9,999 pixels. That way you don't see the text. Then below, I target each of the individual links that are found under each of the individual classes. So I say the link found under the social-twitter class uses the twitter image, the one found under the social-facebook class uses the Facebook image, and so on. And you see that I'm linking the URL for the image to the images folder in the child theme and then in the file in question. When I save this, when I reload the page, you see there's a significant difference.
Instead of having the list of links, I now have usually the icons stacked nicely next to one another and if I click on them, I go directly to that service. But the trick here is I didn't put the images into the HTML; I put text in the HTML and then I used CSS to hide the text and display a background image instead. This is a very common technique used to create buttons that are completely understandable for a browser. So the browser and anyone who doesn't use a visual browser and can't see images will still understand the links without having to mess with the code.
Now, there's one more important thing I have to point out. Remember how we made a custom sidebar for certain pages? Well, if I go to a page now, you'll see that my connect with us links no longer appear. That's because since we already created a custom template for the sidebar widgets for pages, if we want to add these new icons to that template, we have to actively add it into sidebar-page.php as well. Static content in a sidebar is usually reserved for things that shouldn't be changed, and this technique should be used sparingly.
However, in the right circumstances adding static content--whether it be links, images, ads or something else--makes a lot of sense, and now you know how to do just that.
Get unlimited access to all courses for just $25/month.Become a member
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.