admin管理员组

文章数量:1124409

I use WordPress with Polylang PRO and ACF PRO plugins.

Using ACF PRO I've created CPT with post_type 'aktualnosc', with rewrite slug 'aktualnosci' (same slug as page name (with posts list)).

I also use Polylang PRO to translate CPT slugs into other languages.

To fix CPT pagination I use this PHP function.

add_action( 'init', function () {
$post_types = get_post_types( '', 'objects' );

foreach ( $post_types as $post_type ) {
    if ( $post_type->rewrite ) {
        $default_slug = $post_type->rewrite['slug'];

        if ( function_exists( 'pll_languages_list' ) ) {
            $languages = pll_languages_list();

            foreach ( $languages as $lang ) {
                $translated_slug = pll_translate_string( $default_slug, $lang );

                add_rewrite_rule(
                    '^' . $translated_slug . '/page/(\d+)/?$',
                    'index.php?pagename=' . $translated_slug . '&paged=$matches[1]',
                    'top'
                );
            }
        } else {
            add_rewrite_rule(
                '^' . $default_slug . '/page/(\d+)/?$',
                'index.php?pagename=' . $default_slug . '&paged=$matches[1]',
                'top'
            );
        }
    }
}

} );

It works on default CPT slug, but if I click page 2 in pagination on the other language than default CPT slug it causes 'Error 404'.

I've stuck. Have you encountered such a problem and solved it?

本文标签: url rewritingWordPress pagination error when using ACF CPT rewrite slug amp Polylang CPT slug translation