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

functions - Selecting specific query for offset

programmeradmin1浏览0评论

I'm trying to offset the first 3 posts in the functions.php from a query but the next affects all my queries in the homepage:

 add_action('pre_get_posts', 'myprefix_query_offset', 1 );
 function myprefix_query_offset(&$the_query) {

    //Before anything else, make sure this is the right query...
    if ( ! $the_query->is_home() ) {
        return;
    }

    //First, define your desired offset...
    $offset = 3;

    //Next, determine how many posts per page you want (we'll use WordPress's settings)
    $ppp = get_option('posts_per_page');

    //Next, detect and handle pagination...
    if ( $the_query->is_paged ) {

        //Manually determine page query offset (offset + current page (minus one) x posts per page)
        $page_offset = $offset + ( ($the_query->query_vars['paged']-1) * $ppp );

        //Apply adjust page offset
        $the_query->set('offset', $page_offset );

    }
    else {

        //This is the first page. Just use the offset...
        $the_query->set('offset',$offset);

    }
}

Is there any way to simply call one specific query?

Thank you.

发布评论

评论列表(0)

  1. 暂无评论