Disable and redirect pagination of Home

My home is a static page, but Wordpress create enumeration home/page/2/.../3/ and the only solution found that disable pages generation, also redirecting, is the instruction below, but I would like it to have effect only the home page and not the whole site.

Thank you in advance for any suggestions.

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)) {

    setup_postdata($posts[0]);

    $redirect_to = ($ordinal  2) ? '/': (($ordinal  $numpages) ? /$numpages/ : FALSE);

    if(is_string($redirect_to)) {

        $redirect_url = get_option('home') . preg_replace('%'.$matches[0].'%', $redirect_to, $request_uri);

        if($ordinal  2) {
            header($_SERVER['SERVER_PROTOCOL'] . ' 301 Moved Permanently');
        } else {
            header($_SERVER['SERVER_PROTOCOL'] . ' 302 Found');
        }

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

    }
}

Topic homepage pagination Wordpress

Category Web


Taking a cue from a string found in the theme files, I trivially added the following instruction in the function file:

function no_pagination_home() { $paged = bf_get_query_var_paged(); if ( is_home() || ( ( 'page' === get_option( 'show_on_front' ) ) && is_front_page() && bf_get_query_var_paged( 1 ) > 1 )) { header("Location: home"); exit(); } } add_action ('parse_query', 'no_pagination_home');

It does exactly what I was looking for, i.e. it avoids the pagination of Home only by forcing the redirection to it, leaving it in the categories, but with PHP 8, warnings appear on the header which I cannot explain:

Warning: Attempt to read property "ID" on null in /class-wp-query.php on line 4044

Warning: Attempt to read property "post-title" on null in /class-wp-query.php on line 4066

Warning: Attempt to read property "post-name" on null in /class-wp-query.php on line 4048

And the strings involved are:

public function is_page( $page = '' ) {
    if ( ! $this->is_page ) {
        return false;
    }

    if ( empty( $page ) ) {
        return true;
    }

    $page_obj = $this->get_queried_object();

    $page = array_map( 'strval', (array) $page );

    if ( in_array( (string) $page_obj->ID, $page, true ) ) {
        return true;
    } elseif ( in_array( $page_obj->post_title, $page, true ) ) {
        return true;
    } elseif ( in_array( $page_obj->post_name, $page, true ) ) {
        return true;
    } else {
        foreach ( $page as $pagepath ) {
            if ( ! strpos( $pagepath, '/' ) ) {
                continue;
            }
            $pagepath_obj = get_page_by_path( $pagepath );

            if ( $pagepath_obj && ( $pagepath_obj->ID == $page_obj->ID ) ) {
                return true;
            }
        }
    }

    return false;
}

Do you mean only on the first page?

If so, you need that if statement:

if ( !is_paged() ) {
//do stuff only on 1 page
}

If it's not working probably you should create a new variable that will store your home page URL and make another if statement like, if URL is https://yourdomain.com/ then do stuff.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.