• 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 15 replies - 1 through 15 (of 21 total)
  • 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 } ?>’

    Thread Starter martinpetrov

    (@martinpetrov)

    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?

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

    Thread Starter martinpetrov

    (@martinpetrov)

    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…. πŸ™‚

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

    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?

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

    Thread Starter martinpetrov

    (@martinpetrov)

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

    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

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

    Thread Starter martinpetrov

    (@martinpetrov)

    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?

    Kafkaesqui :: your code was good.

    Thread Starter martinpetrov

    (@martinpetrov)

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

    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?

    Thread Starter martinpetrov

    (@martinpetrov)

    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? πŸ™‚

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