Archive for September, 2009

How to make different sidebars for posts, pages

Wednesday, September 16th, 2009

Who has ever made a complicated WordPress setup bump to the problem: how can I make different sidebars to my pages in my theme? I bumped it of course and I found a very simple solution for it. It is very flexible and needs only a basic level of PHP programming so I think anybody can do this with his/her WP theme.

OK, let see the problem and then the solution.

The problem

Normally there is only one sidebar.php in WordPress which you can found in the wp-content/themes/your-theme-name directory. This sidebar.php can be called with the get_sidebar() function. In the default WP setup you can find this function calling in the single.php (displaying a single post), in the page.php (displaying a page) and in the index.php (listing posts). You have only one sidebar.php to modify -if you like- in your WP setup. There are many reasons to use more than one. For example: you have a different front page than your post displaying page, or you would like to differentiate your posts and pages, or you would like to build up a whole site based on the WP very good content management system (CMS).

The solution

I do not like to mixed up the original WP’s operational mechanism thought that I like to upgrade my WP with new versions. Keep this fundamental thing in my mind I make a small modification only in my page.php. I did not bother the single.php and the index.php because these files was good for me with the original sidebar.php.

First of all I made two copy of the sidebar.php. I renamed the first to sidebar1.php and the second to sidebar2.php (I do not want to use my brain capacity to guess a cool name :) )

Then I modified the page.php a bit. Here is the code and than I will give you a explanation.

Comment out the get_siderbar() row in your page.php file and insert this code after it.


switch(true) {
 case is_page('contact'): //Contact
 include(TEMPLATEPATH."/sidebar2.php");
 break;
 case is_page('projects'):
 include(TEMPLATEPATH."/sidebar1.php");
 break;
 case is_page('services'):
 include(TEMPLATEPATH."/sidebar1.php");
 break;
 case is_page('about'):
 include(TEMPLATEPATH."/sidebar1.php");
 break;
 case is_page('privacy-policy'):
 include(TEMPLATEPATH."/sidebar1.php");
 break;
 case is_page('sitemap'):
 include(TEMPLATEPATH."/sidebar1.php");
 break;
 default: get_sidebar();
 break;
}

I do not like to use many ugly if statement so I use a switch. In the switch statement I used the value true to jump in it. Then I used the is_page() WP function to check the pages where I want to use my different sidebars. You can make a very simple to understand code with the is_page() because in the parameter you have to use your page’s or post’s slug. Slug is your shorthand name of your post which makes your site URLs SEO friendly.

The default value of the switch is the get_sidebar() which will call our original sidebar.php.

I think it is very easy and nice. Do it with your themes if you need!

Post me a comment if you use my mod I’m very interested it was easy to implement to others or not.

Separating posts, pages and homepage in WordPress – clean and simple way

Saturday, September 12th, 2009

I played a bit with separating posts, pages and the home page in WordPress today. I tried to find a solution with Google, but there are some tutorials only about the posts and pages separation. I needed something more! And I found the solution what I would like to share with you.

Home page, pages, posts all in one CMS

  • I have a let’s say “independent” home page (the welcome page)
  • I have static pages with static navigation
  • and I would like to write posts so I need:
    • posts listing page
    • single post displaying page

To see the result just look at Eldanor.hu itself. The site have a custom welcome page. There are pages with different sidebars and there are blog posts.

Sounds a bit complicated but it is very easy to implement the whole thing in WordPress. The more good news is that we learned a simple but magical thing about WordPress.

Let’s get started!

Make a homepage template

You can create a HTML or PHP file for your home page depends on your needs. To make it simple call your file homepage.php. Place this file in your currently used template directory (for eg. ./wp-content/themes/templatename).

Add the followings in the beginning of your homepage.php

/*
Template Name: HomePage
*/

You can select this template for your pages!

Do not forget that this “template” contains any PHP code, any HTML or JavaScript – so you are able to create a homepage as you like.

Create your Home page

After you created your HomePage template you can create your Home page. Go to your WordPress admin and there on the left hand side select Pages, and then selec “Add New”. Write Home in the title field and leave everything blank. You have to select a template for your new page. Select the “HomePage” template from the Template dropdown in the Attributes box. (Do you remember? We have created our HomePage template in the first step.) Now you can hit the Publish button!

Create your post listing page

Go to Pages and select “Add New”.  Write Blog in the page title. Leave every other field blank and hit the Publish button.

Do some magic

Now we have two new pages. The first is called Home and the second is called Blog. We joined the Home page and our homepage.php template together.

We have to do some easy steps in the WordPress Admin. Go to Settings and select “Reading”.

How to set your front page in WordPress

How to set your front page in WordPress

You have to select the “A static page” option. Then select your Front page from the dropdown menu – our front page name is Home! Select the Posts page. Our front page name is Blog. Hit the Save changes button and leave WordPress to do the magic!

Now you can see your homepage.php as your homepage (welcome page) and if you like to see your blog posts you have to navigate to blog (for. eg. http://www.yourdomain.com/blog/) and even more you can create pages. All of these are separated and easily managed now from the WP admin!

That’s all!

At the First Time

Thursday, September 10th, 2009

We have started our new site finally! The projects page is waiting for upload all of our past and present projects.  We are very busy in these days about the lot of work but we try to upload all of the information what we have decided.

There are four goals of the Blog section:

  1. to inform our customers about the latest news, changes, projects of our company
  2. to inform our prospect clients our abilities
  3. to make a good collection about articles, tutorials which are inspiring our daily work
  4. of course we wanted to make our own publications to help others making good looking/working websites

We wanted to publish blog posts weekly but if we see an interest we will change this frequency! :)

mjqegd345s