Kakul Sarma
Kakul Sarma

Reputation: 303

Why is have_posts() : the_post() not working

I am trying to display the all post in table With my plugin . I already have three post but following code doesn't display anyting .

CODE OF PLUGIN PAGE :

<?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>
               <tr>
                  <td id="username"><?php the_title(); ?></td>

                  <td>
                      <video class="video" width="200" height="100">
                        <source src="<?php echo $rs->user->profile_video;?>" type="video/mp4">
                        Your browser does not support the video.
                      </video>
                </td>
                  <td id="status"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"></a></td>
                  <td><select class="action">
                      <option>-Select-</option>
                      <option value="1" >Active</option>
                      <option value="0">Inactive</option>
                      <option value="edit">Edit</option>
                      <option value="4">Delete</option>
                    </select></td>
                </tr>


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

Where I have done My Mistake ???

enter image description here

enter image description here

Upvotes: 1

Views: 11271

Answers (2)

user2740557
user2740557

Reputation: 65

I had the same problem,

when i used print_r($wq); i got a result, but have_posts() returned false

i fixed it for me using following code:

<?php
$args = array(
      'posts_per_page'   => -1,
      'offset'           => 0,
      'orderby'          => 'date',
      'order'            => 'ASC',
      'post_type'        => 'post',

    );
$myposts = get_posts( $args );

   foreach ( $myposts as $post ) : setup_postdata( $post );
?>

<p>my html</p>

<?php endforeach; wp_reset_postdata(); ?>

Upvotes: 0

beerwin
beerwin

Reputation: 10327

You can use WP_Query to retrieve your posts. WP_Query is a class and its constructor will return an object, which also has the loop functions.

Notice the $wq variable and $wq->have_posts(), etc.

Important: for the functions called inside the loop (the_title(), the_content(), the_permalink(), etc.) don't prepend the $wq variable. They work as they would with query_posts()

<?php 
    $wq = new WP_Query($args); // $args are the same as the args for query_posts()
?>

<?php if ($wq->have_posts()) : ?><?php while ($wq->have_posts()) : $wq->the_post(); ?>
               <tr>
                  <td id="username"><?php the_title(); ?></td>

                  <td>
                      <video class="video" width="200" height="100">
                        <source src="<?php echo $rs->user->profile_video;?>" type="video/mp4">
                        Your browser does not support the video.
                      </video>
                </td>
                  <td id="status"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"></a></td>
                  <td><select class="action">
                      <option>-Select-</option>
                      <option value="1" >Active</option>
                      <option value="0">Inactive</option>
                      <option value="edit">Edit</option>
                      <option value="4">Delete</option>
                    </select></td>
                </tr>


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

Upvotes: 2

Related Questions