1

PHP noob here. I've got this query I've written to get the information of all child pages of the page you're currently on:

               <?php
                    $pages = get_pages('child_of=1015&sort_column=post_title');
                    $count = 0;

                    foreach($pages as $page)
                    { ?>
                        <div class="position-info"><h4><a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a></h4></div>
                    <?php
                    }
                ?>

But I don't know the correct syntax to turn this into an if statement, followed by an else statement so I can say "There's no content here. Check back later." Or something like that.

This query is inside of the page's main loop.

Can any PHP masters out there help?

0

Answer from an older PHP noob:

<?php

$pages = get_pages('child_of=1015&sort_column=post_title');
$count = 0;

if (!empty($pages)) {
  foreach($pages as $page) {
    // show stuff
  } // end foreach
} else {
  echo '<p>There’s no content here. Check back later.</p>';
}
?>
  • I'm getting a 500 Server Error with that above. I think it has to do with how the else statement starts. My PHP syntax colors aren't jiving in Sublime with that. – andandandandrew Dec 14 '15 at 21:49
  • It's probably because of a forgotten opening or closing <?php or ?> at the wrong place ... i forgot one there when simplifying your code. – Manu Dec 14 '15 at 21:54
0

Here's the code I got working, thanks Manu!

<?php
                    // Vars
                    $pages = get_pages('child_of=1015&sort_column=post_title');
                    $count = 0;

                    if (!empty($pages)) {
                      foreach($pages as $page) { ?>
                        <div class="position-info"><h4><a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a></h4></div>
                    <?php
                    }
                    } else {
                      echo '<div class="position-info"><h4>Bummer! Looks like we&rsquo;re fresh out of internships. Check back often!</div>';
                    }
                ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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