在帖子和页面中禁用分页

时间:2018-05-22 作者:tw8sw8dw8

我需要解决由于意外分页导致的帖子和页面复制问题。我试图使用此功能禁用分页,但似乎遗漏了一些内容:

function wpse_disable_pagination( $query ) {
  if( $query->is_single() && $query->is_page() ) {
    $query->set(\'nopaging\', 1 );
  }
}
add_action( \'pre_get_posts\', \'wpse_disable_pagination\' );

2 个回复
最合适的回答,由SO网友:tw8sw8dw8 整理而成

我找到了正确的解决方案,多亏了Jeff Morris:

global $posts, $numpages;

    $request_uri = $_SERVER[\'REQUEST_URI\'];

    $result = preg_match(\'%\\/(\\d)+(\\/)?$%\', $request_uri, $matches);

    $ordinal = $result ? intval($matches[1]) : FALSE;

    if(is_numeric($ordinal)) {

        // a numbered page was requested: validate it
        // look-ahead: initialises the global $numpages

        setup_postdata($posts[0]); // yes, hack

        $redirect_to = ($ordinal < 2) ? \'/\': (($ordinal > $numpages) ? "/$numpages/" : FALSE);

        if(is_string($redirect_to)) {

            // we got us a phantom
            $redirect_url = get_option(\'home\') . preg_replace(\'%\'.$matches[0].\'%\', $redirect_to, $request_uri);

            // if page = 0 or 1, redirect permanently
            if($ordinal < 2) {
                header($_SERVER[\'SERVER_PROTOCOL\'] . \' 301 Moved Permanently\');
            } else {
                header($_SERVER[\'SERVER_PROTOCOL\'] . \' 302 Found\');
            }

            header("Location: $redirect_url");
            exit();

        }
    }

SO网友:Jignesh Patel
function wpse_disable_pagination( $query ) {
  if( $query->is_single() && $query->is_page() ) {
    $query->set( \'nopaging\' , true );
  }
}
add_action( \'pre_get_posts\', \'wpse_disable_pagination\' );

NOTE: nopaging (boolean) - show all posts or use pagination. Default value is \'false\', use paging.

结束

相关推荐

NEXT_POSTS_LINK在自定义循环中不起作用

我已经为帖子创建了一个自定义循环,它与引导程序一起工作,在其中,我添加了分页,但它似乎不起作用。它只显示一个空按钮。。。<?php $args = array( \'post_type\' => \'post\', \'posts_per_page\' => 1 ); $my_query = null; $my_query = new WP_Query($args);