最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论