1

I am working on a custom theme, basically migrating a current sites look and feel into WordPress. Most of everything is based on Pages, and all of these pages should have a left sidebar. Of those pages, they are one of the 3 below:

  1. Page is part of a section, so shows a list of all parts of the section in a widget
  2. Page is part of a section, so shows a list of all parts of the section in a widget, but also has children, so displays those pages in a seperate widget above.
  3. Page isn't part of a section, so has no sibling or children pages.

I don't want to have to make a separate page template for those that are in #3. Currently, something in #3 won't render anything, except an aside that contains the get_sidebar() function. If this is the case, instead I would like to list all sections, so I have something there.

I can't seem to find any good examples of this. Of all the core functions, they seem to look to see if that sidebar contained widgets in the sense of available in the sidebar - not actually rendered (unless I am missing something). I also tried setting a variable in the widget and read it on the page, but that didn't seem to work either.

How can I conditionally display an extra widget if no others are displayed? Or, if I added this widget to it anyways, hide it if any other widgets are rendered in the same sidebar?

Update: When I state 'none displayed', I mean in such that the widget doesn't echo something back. For example, my widget gets a list of things to display. If the list is empty, I already have conditional logic that doesn't render (or echo anything out) the widget. An example is below:

$subpages = wp_list_pages('title_li=&child_of='.$thisPageID.'&echo=0&depth=1');
if($subpages)
{
    // I have subpages, so display them
    echo $subpages;
}
else
{
     // no subpages, doesn't echo anything. 
}

If it hits the else, and nothing is rendered, im wondering if there is some logic I could use in the sidebar to know I got nothing back, and then render something else to take its place.

Another way of looking at it would be say I have a default widget, a list of everything. But, if I have any other content to display, I hide the default widget, and display that instead. Therefore, every page would have something on it.

Update 2: A friend brought up an idea I previously didn't think of - this widget could just have the opposite of the logic that made it render one of the previous ones - rather than relying on the output of the previous ones. Therefore, since the two would mutually exclusive, all pages would render what they should. In this simple example, that would work. I am still very curious if there is a way to test if a widget echo'd something.

2
  • 1
    What does this mean: "they seem to look to see if that sidebar contained widgets in the sense of available in the sidebar - not actually rendered"? You want to check to see if the installed widget actually echoes something?
    – s_ha_dum
    Jan 28, 2013 at 15:24
  • Yes, that is correct. I want to make sure the widget rendered something that echoes something.
    – jmlumpkin
    Jan 28, 2013 at 16:11

3 Answers 3

1

I not 100% clear what your question is so hopefully this can point you in the right direction for conditional widgets. I will give you 3 options.

1. is_active_widget checks if widgets are active based in the widget ID.

2. Using is_active_sidebar and creating more sidebars usually gives you better control over widget output.

3. Use a plugin, there are several plugins that have post or conditional logic built into them.

4. Try to use the widget_display_callback filter for example:

add_filter('widget_display_callback','your_custom_function',10,3);
2
  • Hi there. I will try to clarify the question a bit more. Your example 1 only checks to see if that widget is used in a sidebar, not actually echoing something. I saw the same as the second answer too. The final answers seem that I would have to keep an index of these pages - and thats what I am trying to avoid.
    – jmlumpkin
    Jan 28, 2013 at 16:14
  • 1
    You might be able to using the widget callback filter, I have updated my answer, but there is probably a better way to do this then checking for output.
    – Wyck
    Jan 28, 2013 at 16:29
0

I'm not a Wordpress expert, but I don't believe it's possible to directly detect empty content like you're suggesting. There are two things I might recommend.

1) My preference: Add some extra logic to your "display parts of the section" widget so it will display sections if the page isn't in a section.

2) Create a new widget that makes use of jQuery. You could then use jQuery to determine if the other widgets have content and display accordingly.

0

You can use the Widget Logic plugin to test if a widget echoes something and change the widget content if needed.

After installed, it allows you to make use of the 'widget_content' filter.

Then, depending on the DOM tree of your widget, you could test if it has proper nodes:

    function check_empty_widget($content='' , $widget_id='')    {
        if ($widget_id == "your-widget-id-1" || $widget_id == "your-widget-id-2" ) {
                $htmldoc = new DOMDocument();
                $htmldoc->loadHTML($content);
                $tag=$htmldoc->getElementsByTagName('ul')->item(0);
                return $tag->hasChildNodes()? $content:'';
        }
        return $content;
}
add_filter('widget_content', 'check_empty_widget',10,2);

It may be possible filter the content with a regular expression also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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