Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Let's say I'm doing a site about cars, and in the main content area of a Page (using a certain template), there are a few paragraphs about a particular car.

In the sidebar, are several standard widgets. But I also want a widget with an 'info panel' about the particular car.

So what's the sanest way of putting in a per-page widget in Wordpress? I guess ideally the info-panel could be entered via the standard page editing in Wordpress .. perhaps via the Custom Fields, so you could enter "Volvo" for the Manufacturer field and it would show up in the sidebar. (or is this something a plug-in already covers?)

share|improve this question
add comment

4 Answers

up vote 2 down vote accepted

You want the Widget Logic Plugin if you're going to use widgets to do this. However, as you have guessed, this is not the best way to do this. I think the best way to achieve what you want to do is to create a custom widget that accesses the posts information if it detects that it's on the correct page. Something like this:

global $wp_query;
$custom_data = get_post_meta( $wp_query->post->ID, 'your_custom_postmeta_key', true );
if( is_page() && !empty( $custom_data ) ){
  echo $before_widget . $before_title . $title . $after_title;
  echo apply_filters( 'the_content', $custom_data );
  echo $after_widget;
}

If you stick that inside the normal widget structure, that would be pretty effective at creating a per-page widget that only appears if you're on a page and that page has the meta content relevant to the widget. At that point, you could just use the custom fields that WordPress already provides on the pages to input the custom content.

share|improve this answer
    
By the same token, rather than using $title, you could add another custom field for the widget title and echo that instead of $title. Or give the custom title precedence over the generic widget title. –  John P Bloch Aug 24 '10 at 13:02
add comment

I can successfully control widget display with the Widget Contexts Plugin For WordPress. I like the way Semiologic Pro is doing it. It's a membership based plugin so no idea if that is what you're looking for:

alt text

It provides a set of display options for all widgets on any sidebar.

share|improve this answer
add comment

There are some plugins that enable you to do that. contextual-widgets for example. The other way is to create a different sidebar for the "page" /"post" template.

A different way is to put a static code in your sidebar.php that checks if you're in "page" or "single" and if so "prints" the info you've entered in the custom fields.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.