loop - How to get current page id and compare it to looped pages inside the same page?

I have a page template named overview.php which inside shows all the pages that are of the same category. As a result al

I have a page template named overview.php which inside shows all the pages that are of the same category. As a result all the pages with the same category are printed but so is the current main page,because it is of the same category as well, and I don't want this to happen. So inside the loop I want to get the ID of every queried page and compare it to current main page ID so that nothing will happen when it is looped.

This is how I query the pages:

$paged = (get_query_var( 'paged' )) ? get_query_var( 'paged' ) : 1;
        $args = array(
            'post_type' => 'page',
            'post_status' => 'Published',
            'category_name' => 'Football League',
            'posts_per_page' => 12,
            'paged' => $paged,
        );
        $arr_posts = new WP_Query( $args );

        if ( $arr_posts->have_posts() ) :

            while ( $arr_posts->have_posts() ) :
                $arr_posts->the_post();
                ?>

prints something about each page..

endwhile;
            wp_pagenavi(
                array(
                    'query' => $arr_posts,
                )
            );
        endif;
        ?>

I have a page template named overview.php which inside shows all the pages that are of the same category. As a result all the pages with the same category are printed but so is the current main page,because it is of the same category as well, and I don't want this to happen. So inside the loop I want to get the ID of every queried page and compare it to current main page ID so that nothing will happen when it is looped.

This is how I query the pages:

$paged = (get_query_var( 'paged' )) ? get_query_var( 'paged' ) : 1;
        $args = array(
            'post_type' => 'page',
            'post_status' => 'Published',
            'category_name' => 'Football League',
            'posts_per_page' => 12,
            'paged' => $paged,
        );
        $arr_posts = new WP_Query( $args );

        if ( $arr_posts->have_posts() ) :

            while ( $arr_posts->have_posts() ) :
                $arr_posts->the_post();
                ?>

prints something about each page..

endwhile;
            wp_pagenavi(
                array(
                    'query' => $arr_posts,
                )
            );
        endif;
        ?>
Share Improve this question edited Sep 5, 2019 at 21:14 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Sep 5, 2019 at 20:17 user174691user174691 1
Add a comment  | 

2 Answers 2

Reset to default 0

The current queried page can be accessed with get_queried_object(), or if you just need the ID, you can use get_queried_object_id().

However, while comparing the current ID in the loop to the queried object ID will allow you to skip a specific post, it won't work well with your pagination, because the page that features the current page will only display 11 pages, rather than the 12 that it's supposed to.

To exclude the current page you can use post__not_in, like this:

$args = array(
    'post_type'      => 'page',
    'post_status'    => 'Published',
    'category_name'  => 'Football League',
    'posts_per_page' => 12,
    'paged'          => $paged,
    'post__not_in'   => get_queried_object_id(),
);

However, if you have a lot of pages, the performance of this is not great.

All that being said, if you're trying to create paginated lists of content, organised by category, then this is not the way I'd go about it. This seems like a good use case for a Custom Post Type and Custom Taxonomy.

You could store the "current" page ID in a variable outside of the loop.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745181008a4615417.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信