SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    while() creating infinite loop

    Hello,

    Happy New Year!

    I'm trying to do something like that:

    PHP Code:
    <?php while ( have_posts() ): ?>
        <p>the_title()</p>
    <?php endwhile; ?>
    And here are my functions:

    PHP Code:
    function have_posts () {
        foreach(
    $posts->all() as $current) {
            
    $thePost->set($current);
        }
        return 
    false;
    }

    function 
    the_title() {
        
    $current $thePost->get();
        echo 
    $current->title;

    Can anyone spot my error?

    Regards,

    -jj.

  2. #2
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    989
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    The problem is that each iteration of while() evaluates have_posts() so every time the condition is checked it runs the foreach loop. This would be a simple fix:

    PHP Code:
    <?php foreach ( have_posts()  as $post): ?> 
        <p><?php echo $post->title;?></p> 
    <?php endwhile; ?>
    PHP Code:
    function have_posts () { 
        return 
    $posts->all();


  3. #3
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks for the reply

    What about a WP-like approach? Would you know how to do that?

    <?php while ( have_posts() ): the_post()): ?>

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    You'd have to have a parameter to the function, which would fairly degenerate the while into a for.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •