• Hi,
    Why when i run this function the pagination doesn’t work.
    Error 404.
    if you have any suggestions, I’m interested.
    Thank you beforehand

    function filter_post_link($permalink, $post) {
        if ($post->post_type != 'post')
            return $permalink;
        return 'the-blog'.$permalink;
    }
    add_filter('pre_post_link', 'filter_post_link', 10, 2);
    
    add_action( 'generate_rewrite_rules', 'add_blog_rewrites' );
    function add_blog_rewrites( $wp_rewrite ) {
      $wp_rewrite->rules = array(
        'the-blog/([^/]+)/?$' => 'index.php?name=$matches[1]',
        'the-blog/[^/]+/attachment/([^/]+)/?$' => 'index.php?attachment=$matches[1]',
        'the-blog/[^/]+/attachment/([^/]+)/trackback/?$' => 'index.php?attachment=$matches[1]&tb=1',
        'the-blog/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
        'the-blog/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
        'the-blog/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?attachment=$matches[1]&cpage=$matches[2]',
        'the-blog/([^/]+)/trackback/?$' => 'index.php?name=$matches[1]&tb=1',
        'the-blog/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?name=$matches[1]&feed=$matches[2]',
        'the-blog/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?name=$matches[1]&feed=$matches[2]',
        'the-blog/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?name=$matches[1]&paged=$matches[2]',
        'the-blog/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?name=$matches[1]&cpage=$matches[2]',
        'the-blog/([^/]+)(/[0-9]+)?/?$' => 'index.php?name=$matches[1]&page=$matches[2]',
        'the-blog/[^/]+/([^/]+)/?$' => 'index.php?attachment=$matches[1]',
        'the-blog/[^/]+/([^/]+)/trackback/?$' => 'index.php?attachment=$matches[1]&tb=1',
        'the-blog/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
        'the-blog/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
        'the-blog/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?attachment=$matches[1]&cpage=$matches[2]',
      ) + $wp_rewrite->rules;
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Are you sure the rewrite rules are actually added? ‘generate_rewrite_rules’ does not fire on every request. Toggle your permalink settings in Settings>Permalinks to Plain and back again to ensure they are added. If your permalink structure is custom, copy it somewhere first because it’ll disappear.

    For non-paginated posts you could insert ‘the-blog’ even without the added rules, but doing so along with pagination will only work with the added rules.

    If you’ve done that, please provide an example URL that doesn’t work. A quick test on my site with mydomain.com/the-blog/my-multi-page-post/page/2/ worked fine with your code.

    along with above refer this link for details about url rewrite

    https://codex.www.ads-software.com/Class_Reference/WP_Rewrite

    Thread Starter Cryback78

    (@cryback78)

    Hi, Thank you for your answers.
    So I tried on an empty wordpress with only the above routine, but it does not work like this. if the name of the page is different from the-blog then yes it works. But if the page is called the-blog then error 404

    With page name toto (local host)
    https://www.test.fr/the-blog/2014/10/04/demo-post-38/
    With page name the-blog (local host)
    https://www.test.fr/2/ (local host) error 404

    Moderator bcworkz

    (@bcworkz)

    I believe it’s not possible to have a post slug also serve as a different permalink element. Sort of like how we cannot have a workable post slug of “wp-admin”.

    What are you trying to accomplish with this? Perhaps there’s another way to approach this. What is so special about this “the-post” page? Why must it have a slug the same as a permalink element?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Pagination url rewriting’ is closed to new replies.