Leff
Leff

Reputation: 1320

Php while loop in WP not working

I would like to loop through posts in wordpress, but I am doing something wrong with my loop, since I get an error for it, not sure how to fix it. This is the loop:

  <?php if ($wp_query->have_posts()) : global $wp_query; $count = $wp_query->found_posts; ?>
      <?php  while ( $wp_query->have_posts()) : $wp_query->the_post(); ?>


            <li> <?php echo get_the_title() ?></li>

      <?php endwhile; ?>
    <?php endif; ?>

I get an error

page isn’t working

superselma.dev is currently unable to handle this request. HTTP ERROR 500

Updated

When I have it like this:

<?php if ($wp_query->have_posts()) :?>
          <?php if ($wp_query->have_posts()) : global $wp_query; $count = $wp_query->found_posts; ?>
        <?php  while ( $wp_query->have_posts()) : $wp_query->the_post(); ?>


              <li> <?php echo get_the_title() ?></li>

        <?php endwhile; ?>
 <?php endif; ?>

Then it is working. So, the problem is in this line:

<?php if ($wp_query->have_posts()) : global $wp_query; $count = $wp_query->found_posts; ?>

This is the full code of the page:

<?php if ($wp_query->have_posts()) :?>
          <?php if ($wp_query->have_posts()) : global $wp_query; $count = $wp_query->found_posts; ?>
        <?php  while ( $wp_query->have_posts()) : $wp_query->the_post(); ?>


              <li> <?php echo get_the_title() ?></li>

        <?php endwhile; ?>
      <?php endif; ?>

        <?php if ($count > $per_page) :?>
            <div id="target-area"></div>

            <div class="small-centered small-10 medium-6 columns">
                <a id="get-more" class="sfk-btn button expand" href="javascript:getMore(<?php echo $cat_id; ?>, <?php echo $per_page; ?>, <?php echo $per_page; ?>)"><span>Klikk for mer</span></a>
            </div>
        <?php endif; ?>

        <?php else : ?>

            <article id="post-0" class="post no-results not-found">
                <header class="entry-header">
                    <h1 class="entry-title"><?php _e( 'Ikke flere artikler', 'twentyeleven' ); ?></h1>
                </header><!-- .entry-header -->

                <div class="entry-content">
                    <p><?php _e( 'Beklager, men ingen poster var funnet for dette arkivet.', 'twentyeleven' ); ?></p>
                    <?php get_search_form(); ?>
                </div><!-- .entry-content -->
            </article><!-- #post-0 -->

        <?php endif; ?>

Upvotes: 1

Views: 366

Answers (4)

Leff
Leff

Reputation: 1320

The problem was that I was closing the if loop before I should have. When I removed first endif statement then it worked.

Upvotes: 1

Paul
Paul

Reputation: 44

first of all, could you look into php error log, if not defined config it in php.ini It helps a lot, also set wordpress into debug mode.

http://php.net/manual/en/errorfunc.configuration.php#ini.error-log

Upvotes: 1

Cyto
Cyto

Reputation: 60

Declare (or if exists remove) global $wp_query; before if statement and remove endif; after div class="small-centered small-10 medium-6 columns"

Upvotes: 1

Rakesh Suryavardhan
Rakesh Suryavardhan

Reputation: 56

Remove global $wp_query; from your code. It will start printing the title.

Upvotes: 1

Related Questions