WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to loop all category posts except the open one in wordpress? (3 posts)

  1. modeman
    Member
    Posted 1 year ago #

    in my single.php i want to show all category posts except the opened post.

    Here is my code: pastebin.com loop code

    Here is the code for category posts loop, it loops all category posts and the opened one, i do not want to loop opened post again

    <?php $catid = the_category_ID( false ); ?>
                            <?php $postCount = 1; $loop = new WP_Query( array( 'tax_query' => array(array(
                                    'taxonomy' => 'category',
                                    'field' => 'id',
                                    'terms' => $catid
                                )), 'post_type' => 'post', 'posts_per_page' => 15 ) ); if ($loop->have_posts()) { ?>
  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    use 'post__not_in' http://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters

    depending on where the code is, you might need to add global $post; before the query;

    also review: http://codex.wordpress.org/Function_Reference/the_category_ID

    <?php $catid = get_the_category(); $catid = $catid[0]->term_id; ?>
                            <?php $postCount = 1; $loop = new WP_Query( array( 'tax_query' => array(array(
                                    'taxonomy' => 'category',
                                    'field' => 'id',
                                    'terms' => $catid
                                )),
                                'post_type' => 'post',
                                'posts_per_page' => 15,
                                'post__not_in' =>array($post->ID) ) );
                            if ($loop->have_posts()) { ?>

    untested

  3. modeman
    Member
    Posted 1 year ago #

    Thanks, works like i wanted to work :)

Topic Closed

This topic has been closed to new replies.

About this Topic