0

On my single.php i want to show the current post and underneath it i want to list all the posts in cat-2. In my loop i tried to query post with cat-2 but it still shows the current post.

<?php global $query_string;
$posts = query_posts($query_string.''); ?>
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
                <h4><?php the_title(); ?></h4>

            <?php endif; ?>

            <?php the_content(); // Dynamic Content ?>
            <?php edit_post_link(); ?>    
    <?php endwhile; ?>
    <?php else: ?>
            <h1><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h1>

    <?php endif; ?>
<?php rewind_posts(); ?>

<hr>
<?php global $query_string; // required
$posts = query_posts($query_string.'&cat=2,&order=ASC'); ?>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <a href="<?php the_permalink(); ?>"><img title="<?php the_title(); ?>" alt="<?php the_title(); ?>" class="wp-post-image" src="<?php the_post_thumbnail_url(); ?> " style="width:100%; height:auto;"></a>
        <h4><?php the_title(); ?></h4>
    </div>
</div>

<?php endwhile; ?>

<?php else : ?>

<?php endif; ?>

<?php wp_reset_query(); // reset the query ?>
0

List of post of a category can be show using wp_query like this

$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
        'post_type' => 'post',  //Specifying post type
        'posts_per_page' => 10, //How many posts per page
        'cat' =>'cat2',         //Specifying post category to show posts
        'paged' => $paged       //For pagingation (if required)
        );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>     
    <h3><?php the_title();?></h3>   
<?php 
endwhile; ?>
| improve this answer | |
0

nstead of using query_posts use wp_query:

     <?php
        $args = array('post_type' => 'your-post-type-name'
                      'cat'=>4,
                      'order'=>'ASC'
                     );
        $the_query = new WP_Query($args); // required
            // The Loop
        if ($the_query->have_posts()) {
            echo '<ul>';
            while ($the_query->have_posts()) {
                $the_query->the_post();
                echo '<h4>' . get_the_title() . '</h4>';
            }
            echo '</ul>';
            /* Restore original Post Data */
            wp_reset_postdata();
        } else {
            echo "no posts found";
        }
        ?>
| improve this answer | |

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.