开发者

Wordpress Conditional Statement

开发者 https://www.devze.com 2023-02-21 18:04 出处:网络
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:

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; ?>
        &l开发者_Python百科t;/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.


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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消