WordPress.org

Support

Support » Themes and Templates » if page has subpages … else …

if page has subpages … else …

  • I want to display a page’s subpages if it has any, and if there are no subpages, i want to display some text.

    Im trying this code but can not get it to work, it keeps displaying the “text if else” even if there ARE subpages. Help?

    <ul>
      <?php
    
    global $id;
    $vm = wp_list_pages("title_li=&child_of=$id&show_date=modified&date_format=$date_format");
    if ($vm) {$vm;}
    else { echo 'text if else'; };
    
    ?>
    </ul>
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator keesiemeijer

    @keesiemeijer

    try it with this:

    <?php
    	global $post;
    	$vm = wp_list_pages("title_li=&child_of=$post->ID&echo=0&show_date=modified&date_format=$date_format");
    	if ($vm != '') {
    		echo '<ul>' . $vm . '</ul>';
    	} else {
    		echo '<p>text if else</p>';
    	};
    ?>

    Moderator Michael

    @alchymyth

    Forum Moderator

    general idea:

    read the codex pages for a given function if you encounter problems:

    in this case:
    http://codex.wordpress.org/Function_Reference/wp_list_pages

    i.e. the use of the ‘echo’ parameter if you want to use the function output in a conditional statement.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘if page has subpages … else …’ is closed to new replies.