pindol
pindol

Reputation: 2110

Display current post after showing posts query result in wordpress

In sigle.php page I need to display a list of post that belong to the category of the post and the I have to display the post content.

So I did a query to get the posts tat belong to the post category:

<?php
$cat = get_the_category();
query_posts('cat='.$cat[0]->cat_ID);
while (have_posts()) : the_post();?>
    <li <?php echo get_the_ID() == get_query_var('p') ? 'class="current-menu-item"' : '';?>><a href="<?php echo the_permalink();?>"><?php echo get_the_title();?></a></li>
<?php endwhile;?>

Now how can I retrive the current post data? If I do like this

<?php while ( have_posts() ) : the_post(); ?>
<?php echo the_post_thumbnail(get_the_ID());?>
<?php endwhile;?>

I still display query_posts.

Upvotes: 0

Views: 86

Answers (1)

pindol
pindol

Reputation: 2110

I solved using WP_Query instead of query_posts();

<?php
    $query = new WP_Query('cat='.$cat[0]->cat_ID);
    while ($query->have_posts()) : $query->the_post();?>
        <li <?php echo get_the_ID() == get_query_var('p') ? 'class="current-menu-item"' : '';?>><a href="<?php echo the_permalink();?>"><?php echo get_the_title();?></a></li>
    <?php endwhile;?>

Upvotes: 1

Related Questions