WordPress.org

Ready to get started?Download WordPress

Ideas

Per-Post Sidebar (or Widget)

  1. Bartje
    Member

    12345

    I would like to be able to put some post-specific things in the sidebar such as, e.g., links relevant to this particular post, an image specific for this post, etc. (basically just any piece of HTML as in the post content)

    This means part of the sidebar content would remain independent of the post being viewed and part would be specific for the post you are viewing.

    The way I do this now is to remove the sidebar from single.php and use a two-column layout for the post content. Besides the obvious disadvantage of loosing the sidebar content this has the additional drawback that I need to include the 'static' part (i.e. the part that is common across all posts) in all the posts. And as a consequence, if it needs to be changed I need to change it in all the posts...

    Bart.
    ps I think (didn't try it yet) I could make this work already using a custom field for the per-post sidebar content and a specific sidebar.php to call from single.php but it would be much easier if such a capability were built-in (especially in terms of future maintenance)

    Posted: 7 years ago #
  2. melilyn
    Member

    12345

    The only problem is that this... well, it can be done already and it's more theme dependent. It's built in to some themes and would have to be built into every other theme. It's not really a core wordpress thing because... well, you can already do it. Does that make sense? :/

    Like, the Blix theme, really popular theme, has been around for a while... it has a php snippet in the sidebar that calls in certain sections of the sidebar in for specific pages. For example, if you're on an archive page, you'll get the calendar, but not on the main page or anything else.

    Posted: 7 years ago #
  3. Mark
    Member

    12345

    Agree with melilyn: this is typical theme logic. You can already do this because the Sidebar Widgets plugin supports multiple sidebars and because you can use conditionals in sidebar.php. The implementation you suggest (using custom fields and catching the content of those in sidebar.php is indeed the way to go.)

    I've implemented something like this on DeResearcher.nl, where the sidebar displays either a calendar, a poll, a list of sub- and sibling-categories, or a list of recent similar posts depending on what kind of page your looking at (archive, home, category, and post, respectively).

    I disagree that this capability should be built in; it's too much of a niche functionality to be included in the core.

    Posted: 7 years ago #
  4. hey there. i think this person means a sidebar specific to one post, not single posts themselves. I've accomplished this, by using advanced writing features, and a key/custom field with html in it. the custom key was called "sidebar", and the I just said:
    (get_post_meta($post->ID, sidebar, true))
    within an include on the sidebar, when various somesuch if / then statements were true. I woudl give you a link, but i'm still working on this site, which uses wordpress as a CMS for my new portfolio site.... will post it when done.

    Posted: 6 years ago #
  5. andy_woz
    Member

    12345

    This may help someone somewhere.
    Throw all the code out of the standard sidebar and replace with

    <div id="sidebar">
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post();
    $sidebar = get_post_meta($post->ID, 'sidebar', $single = true)
    ?>
    <div>
    <?php // if there's a sidebar content
    if($sidebar !== '') { ?>
    <p>
    <?php echo $sidebar; ?>
    </p>
    <?php } // end if statement

    // if there's no sidebar content
    else { echo ''; } ?>
    </div>
    <?php endwhile; ?>
    <?php else : ?>
    <p class="center">Sorry, but you are looking for something that isn't here.</p>
    <?php endif; ?>
    </div>

    Then add a custom key in your page called 'sidebar' and have at it!

    You may want to leave the pages navigation in there somewhere.

      <?php wp_list_pages('title_li=<h2>Pages</h2>' ); ?>
    Posted: 6 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.

  • Rating

    12345
    52 Votes
  • Status

    This is plugin territory