0

Im trying to create a widget that displays a list depending on whether or not the page has children. Here is what my code looks like so far:

<?php    
function widget_myHelloWorld() {
    ?>
    <?php if ( is_page() ) { ?>
    <?php
    global $id;
    $children = wp_list_pages("title_li=&child_of=$id&show_date=modified&date_format=$date_format");
    if ($children) {?>
        <ul>
    <?php echo $children; ?>
        </ul>
    <?php } } ?>
    <?php
    }

    function myHelloWorld_init()
    {
      register_sidebar_widget(__('Sidebar Sub Navigation'), 'widget_myHelloWorld');
    }
    add_action("plugins_loaded", "myHelloWorld_init");
    ?>

The <li> elements containing the links to sub-pages are successfully displayed, however the elements are not. Any idea what is going on here?

*Eventually, I need to add some divs and other elements, so simply inserting the extra code as part of the wrap parameter in wp_list_pages() is impractical.

2
  • Could you clarify your second to last paragraph? Commented Apr 4, 2011 at 11:43
  • "however the elements are not" Witch elements? You could paste a print_r($children) and HTML output that you say is not working.. Commented Apr 5, 2011 at 2:34

1 Answer 1

1

wp_list_pages() will always output the pages list. Even if you try to assign it to a variable. It's the equivalent of $children = echo 'string'; which isn't going to work.

what you wanna do is define:

$children = get_pages('child_of='.$post->ID);

Then to check it conditionally

if (count($children) != 0 ) {
    //this is where the magic happens
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.