WordPress.org

Ready to get started?Download WordPress

Forums

Custom Sitebar per Page - solution? (22 posts)

  1. martinpetrov
    Member
    Posted 8 years ago #

    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...

  2. WarAxe
    Member
    Posted 8 years ago #

    Why not just put some conditions in your sidebar.php? If you were unconfortable putting ALL your code in sidebar you could even break it out into individual php files and then conditional include them.

    '<?php if (strpos($_SERVER[REQUEST_URI],'gallery') { ?>'

    ' <!-- do something... -->'

    ' <?php include("gallery_sidebar.php") ?>'

    '<?php } ?>'

  3. martinpetrov
    Member
    Posted 8 years ago #

    Hi, thanks for the suggestion...
    I could do it the way you say, but as long I understand then I have to manually add these conditions for every different page I want to include. The problem is that I'm building a site for a client who is dump :) I don't want to maintain the website for her once it's done.

    I could ask the client to wrap the text that she wants to appear in the sidebar with a <div id="sidebar"></div>, however it is too much for her. And I also noticed the WYSIWYG editor makes some changes with the divs and it breaks the design.

    It would be really easy for the client to create a page with a name "currentpage_sidebar" and not to worry about anything.

    What do you think?

  4. WarAxe
    Member
    Posted 8 years ago #

    If the client is dumb how are they going to make their own sidebars?

  5. martinpetrov
    Member
    Posted 8 years ago #

    I will tell the client to create a new page with the same name as the page where the sidebar will appear + sidebar at the end of that name. For example: there is a page named "contact" and if she wants a sidebar there she will create a new page named "contactsidebar".

    Creating pages in WordPress is really easy.. My whole website is made of static pages listed in the navigation menu (using a single template file).

    As I said, I'm looking for a php code that includes pages named after the current page + some extension (contact + sidebar = contactsidebar).

    I was astonished to see the "client" doesn't know how to rename files, or copy them or move.... :)

  6. Kafkaesqui

    Posted 8 years ago #

    This may make it simpler:

    <?php
    global $post;
    $sidebar_template = $post->post_name . '-sidebar.php';
    if(file_exists(TEMPLATEPATH . "/$sidebar_template")) {
    include($sidebar_template);
    }
    ?>

    Format for the sidebar template file names is:

    title-of-page-sidebar.php

    This of course does not cover WarAxe's concern. :/

  7. maelgwn
    Member
    Posted 8 years ago #

    I think if the sidebar page exists with the same name format always, as in pagesidebar, then you should be able to get the name of the page and just add sidebar to it and include. Something along these lines (in the sidebar)

    <?php
    include ($_SERVER[REQUEST_URI]'sidebar')
    ?>

    Maybe testing if the page exists first would be a good idea. The problem I see is how to get the sidebar contents formatted correctly?

  8. WarAxe
    Member
    Posted 8 years ago #

    Sounds like a great opportunity to charge the "client" a hefty monthly hand-holding fee.

  9. martinpetrov
    Member
    Posted 8 years ago #

    Thanks for the suggestions..It seems I'm dump too though. I thought those WordPress Pages don't exist for real. They are not real .php pages.

    There is a plugin called "Improved Include Page". With it and the follwing code you can include a wordpress page: <?php if(function_exists('iinclude_page')) iinclude_page('51'); ?>. With it I have currently included a sidebar page, but it is static. It's on the right side( see the url under)

    I see you know php :) could you modify the code for the include plugin to look for the name of the current page + some extension? Or it doesn't work like that?

    About the "client". It is not a real client, I'm not paid. It is an internship and I will get credits for school. It's my first web site. Take a preview at http://speakingofresults.com/ (under construction...)

  10. maelgwn
    Member
    Posted 8 years ago #

    i think my code does what you want, it would be labelled by the name of the page followed by sidebar, not 100% sure if it would work tho

  11. Kafkaesqui

    Posted 8 years ago #

    maelgwn, there is a problem with your suggestion. A Page on a blog with default permalinks would return something like:

    /index.php?page_id=100sidebar

    while custom permalinks would return:

    /page-name/sidebar

    With the latter one could clip off the slashes (see trim()), but those with default permalinks require a different solution (unless one is planning to use 100sidebar.php for the template name).

    martinpetrov, they are not *real* (or rather, built-in) WordPress templates, hence my use of if(file_exists()) to test if the template file is there.

    The "include" page plugins (similar to my Get-a-Post) call up and display a WordPress Page's content, and not a template or other PHP document. Don't let the Include in their names fool you on that point.

    So the question is, are you trying to include a page (as in a *.php document), or the content of a Page (as in Write > Write Page)?

  12. martinpetrov
    Member
    Posted 8 years ago #

    I'm trying to include the content of a Page.

    I was thinking, maybe the name of that Page to be included can be just "sidebar" instead of current_page_name + sidebar. And there will php include code sniffing for a subpage called sidebar. Is it possible?

  13. WarAxe
    Member
    Posted 8 years ago #

    Kafkaesqui :: your code was good.

  14. martinpetrov
    Member
    Posted 8 years ago #

    Kafkaesqui, is it possible to do what I want if I'm trying to include the content of a Page? I'm confused.

  15. Kafkaesqui

    Posted 8 years ago #

    martinpetrov, I think we've crossed wires. Originally you asked how to retrieve the name of the Page you're on to include a custom sidebar template file based on that name. This is a lot different than including content from a WordPress Page, as Pages and posts and whatnot don't exist as physical files residing on the server (it's all in the database).

    The plugin you referred to (as well as mine) can be used to include Page content wherever you use it. But I'm not sure how the name of the current Page is of use for calling this other Page, unless the other Page is called "somethingsidebar" and its content contains the actual sidebar elements you're looking to *import*. Is this the case?

  16. martinpetrov
    Member
    Posted 8 years ago #

    Sorry Kafkaesqui, I wasn' very clear. English is not my first language, and I also started with WordPress a few days ago.
    This is the case as you suggested in your last post. However...

    Now I don't think I need the name of the current Page. The "slug" is only what is needed, I guess.

    Can your plugin include a Page with a name (slug?) "sidebar" which is a subpage of the current? Maybe if it can get the requested url and add "/sidebar/" at the end it will include the desired Page.

    And then ,for example the content of my Page named "sidebar" will be included in it's ancestor page.

    Or maybe maelgwn's code is all what is needed:
    <?php include ($_SERVER[REQUEST_URI]'sidebar') ?>

    I couldn't make it work. Does maelgwn's code look for a Page or a page? :)

  17. Kafkaesqui

    Posted 8 years ago #

    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).

  18. martinpetrov
    Member
    Posted 8 years ago #

    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.

  19. Kafkaesqui

    Posted 8 years ago #

    "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. ;)

  20. maelgwn
    Member
    Posted 8 years ago #

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

  21. martinpetrov
    Member
    Posted 8 years ago #

    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...

  22. ekrocker
    Member
    Posted 8 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.