Support » Fixing WordPress » Custom Sitebar per Page – solution?

  • Hey, I would like someone with php knowledge to help me create a custom sidebar per page.
    I have an idea…

    In my main template (I use a single template for everything) I want to include a page which I will style with CSS as a sidebar. I want the include php command to search for a page named after the current page + sidebar at the end. For example if you are viewing the “about” page, then a file called “aboutsidebar” (or something) is included.

    I hope I’m clear. It sounds easy but I don’t know php.

    My question is – how do you get the name of the current page? So basically what is the php code for including a file with a name taken from the current page + sidebar at the end?
    And in case the file to be included doesn’t exist, does php create a problem?

    Sory, I’m a beginner…

Viewing 6 replies - 16 through 21 (of 21 total)
  • No need to apologise.

    Can your plugin include a Page with a name (slug?) “sidebar” which is a subpage of the current?

    Maybe. If the subpage has a Page name or slug of “parent-page-namesidebar” (or “parent-page-name-sidebar,” or whatever as long as the parent’s name is in there), I can code up a little something to drop in Get-a-Post that would work. But if each subpage is just “sidebar,” Get-a-Post would hit the first one listed in the database, and with two or more it becomes a problem.

    In either case let us know exactly what the naming scheme will be for these sidebar Pages. Should be able to come up with a solution, even with “sidebar” as the name for each.

    By the way, we can put aside the suggestions for using include(). It won’t work in this context, since its purpose is for including physical files (i.e. pages, not Pages).

    Kafkaesqui,
    I really appreciate your help… especially since it is for free 🙂

    A naming scheme “parent-page-name-sidebar” is good. Don’t do it with a same name for each Page if only the first one in the database is hit. The user, even though not competent, will be told where to look for the page slug before creating a sidebar Page (it’s easy enough).

    Btw, I really enjoy your web site. The font families and sizes are perfect, and the the color scheme too. It has a unique look and at the same time it gives a professional feeling. I would like to see more sites like yours, but creating new layouts is difficult. I admit I have copied most of the elements on the site I’m building.

    I really appreciate your help… especially since it is for free 🙂

    You get what you pay for. :D

    Dealing with the topic at hand, your ‘client’ could simply be told to title these sidebar sub-pages “Parent Page Title sidebar” — that’s it. If that title format is adhered to WordPress takes care of the rest.

    For the code part, using my plugin you want this in the sidebar template:

    <?php
    global $post;
    get_a_post($post->post_name . '-sidebar');
    if($post->post_content) {
    the_content();
    }
    ?>

    That’s a basic example displaying the content of the subpage you’re calling (the if on $post->post_content is to make sure the subpage exists). Keep in mind you can use any standard Template Tag intended for The Loop here, or through the $post object (and PHP) interact with the data related to the subpage.

    Thank you for your comments in regards to my site. “Casual-professional” is a look I was going for, so I appreciate your feelings on it. In defense of truly creative people everywhere, let me note I’ve been at this for so long I’ve become very good at *borrowing* and putting design features to use in such a way one is fooled into thinking they are unique. One of this particular hack’s better talents. ;)

    Kafkaesqui, thanks for the work there, isee my problem …

    Well it’s stil doesn’t work. The code displays the content of my Home Page.
    see http://speakingofresults.com
    (the “client” requested to hide the page from the “public” with a passowrd. User and pass are both “123”)

    I have a static front page (with a plugin) which is actually a Page called Home. This page is loaded in the sidebar 🙁

    I’m afraid I have to abandon my idea for a custom sidebar and your efforts. It seems I have to learn more about WordPress before implementing complicated things…

    I always thought there was a custom field called “sidebar” or something that you would set within each page. Am I wrong? I think that would be easier.

Viewing 6 replies - 16 through 21 (of 21 total)
  • The topic ‘Custom Sitebar per Page – solution?’ is closed to new replies.