WordPress.org

Support

Support » How-To and Troubleshooting » if has child pages display them, if not do something else

if has child pages display them, if not do something else

  • In order to reduce the number of template in my theme I am using some conditional statements to change things on my pages. I am currently using this in my sidebar:

    <?php
    $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
    if ($children) { ?>
    <h2><?php $parent_title = get_the_title($post->post_parent); echo $parent_title; ?> Pages</h2>
    <ul>
    <?php echo $children; ?>
    </ul>
    <?php } else
    wp_get_archives();
    ?>

    Basically this checks to see if the page has child pages, and if so it lists the child page links. If it doesn’t have a child page then it list the archives.

    My problem is, is that I want it to list the posts titles from a certain category in an unordered list (wrapped in <ul> tags so that it displays correctly) if the page doesn’t have child pages.

    Can anyone help me out on how to do this. The codex is full of example of conditional uses but none quite seem to match what I am looking for.

    Thanks in advance.

Viewing 1 replies (of 1 total)
  • what about a simple loop:

    <?php
    query_posts("cat=xx");
    if( have_posts() ):
    ?>
    <ul>
    <?php
    while( $i=1; while (have_posts()) : the_post();
    ?>
    <li>
    <a href="<?=the_permalink()?>" title="<?=the_title()?>">
    <?= the_title(); ?>
    </a>
    </li>
    <?php endwhile; ?>
    </ul>
    <?php endif; ?>

Viewing 1 replies (of 1 total)
  • The topic ‘if has child pages display them, if not do something else’ is closed to new replies.
Skip to toolbar