0

I would like to know if is possible to add a "subpage" to a post and in this "subpage" have paginated content, for example:

I Have a Post Lorem Ipsum
This post has Videos, Photos and Audios as attachments
When you access the post www.website.com/lorem-ipsum/ you access basically just the text, and some other infos and you could access:

  • www.website.com/lorem-ipsum/videos/
  • www.website.com/lorem-ipsum/videos/page/2
  • www.website.com/lorem-ipsum/audios/
  • www.website.com/lorem-ipsum/audios/page/2
  • www.website.com/lorem-ipsum/photos/
  • www.website.com/lorem-ipsum/photos/page/2

I have this on my functions.php

<?php 

add_action( 'after_setup_theme', 'rm_setup_theme' );

function rm_setup_theme() {

    add_filter( 'rewrite_rules_array', 'rm_custom_rules' );
    add_filter( 'query_vars',          'rm_custom_vars' );
    add_filter( 'template_redirect',   'rm_custom_redirect' );
}

function rm_custom_rules( $rules ) {

    $r = array( 
        '([^/]+)(/[0-9]+)?/photos/page/?([0-9]{1,})/?$' => 'index.php?name=$matches[1]&paged=$matches[2]&photos=1',
        '([^/]+)(/[0-9]+)?/photos/?$'                   => 'index.php?name=$matches[1]&photos=1'
    );

    return array_merge( $r, $rules );
}

function rm_custom_vars( $qv ) {
    array_push( $qv, 'photos' );
    return $qv;
}

function rm_custom_redirect( ) {
    global $wp_query;
    $f = $wp_query->get( 'photos' );

    if ( $f ) {
        require TEMPLATEPATH . '/content-photos.php';
        exit;
    }
}

?>

With this I have the following urls "working":

  • www.website.com/lorem-ipsum/photos/
  • www.website.com/lorem-ipsum/photos/page/2

But the pagination it's not working, it looks like the paged var it's not passed, it always show the content of the first page, no matter what page do you type in the address bar

/photos Request
The request on photos page (www.website.com/lorem-ipsum/photos/):

Request:
lorem-ipsum-dolor-sit-amet/photos

Query String:
name=lorem-ipsum-dolor-sit-amet&photos=1

Matched Rewrite Rule:
([^/]+)(/[0-9]+)?/photos/?$

Matched Rewrite Query:
name=lorem-ipsum-dolor-sit-amet&photos=1

/photos/page/2 Request
The request on photos page (www.website.com/lorem-ipsum/photos/page/2):

Request:
lorem-ipsum-dolor-sit-amet/photos/page/2

Query String:
name=lorem-ipsum-dolor-sit-amet&photos=1

Matched Rewrite Rule:
([^/]+)(/[0-9]+)?/photos/page/?([0-9]{1,})/?$

Matched Rewrite Query:
name=lorem-ipsum-dolor-sit-amet&paged=&photos=1

Thanks

1

After more research on and

I got introduced to Redirect Canonical

Redirects incoming links to the proper URL based on the site url.

Search engines consider www.somedomain.com and somedomain.com to be two different URLs when they both go to the same location. This SEO enhancement prevents penalty for duplicate content by redirecting all incoming links to one or the other.

And because of that all the paginated pages where been redirected to the domain.com/lorem=ipsum/photos when I wanted domain.com/lorem-ipsum/photos/page/2

Because of that I needed to change the redirect_canonical with the filter redirect_canonical

PS: If you wrote a good rewrite rule, but it's taking you to another page It can be redirect_canonical acting on your rewrite rule

I hope it helps anyone else, sorry for bad english.

You can check my final functions.php

add_action( 'after_setup_theme', 'rm_setup_theme' );

function rm_setup_theme() {
    add_filter( 'rewrite_rules_array', 'rm_custom_rules' );
    add_filter( 'query_vars', 'rm_custom_vars' );
    add_filter( 'template_redirect', 'rm_custom_redirect' );
    add_filter( 'redirect_canonical', 'rm_redirect_canonical', 10, 2 );
}


function rm_custom_rules( $rules ) {
    $r = array( 
        '([^/]+)(/[0-9]+)?/photos/page/?([0-9]{1,})/?$' => 'index.php?name=$matches[1]&page=$matches[2]&photos=1&paged=$matches[3]',
        '([^/]+)(/[0-9]+)?/photos/?$'                   => 'index.php?name=$matches[1]&page=$matches[2]&photos=1',
    );
    return array_merge( $r, $rules );
}


function rm_custom_vars( $qv ) {
    array_push( $qv, 'photos' );
    return $qv;
}

function rm_custom_redirect( ) {
    global $wp_query;

    $f = $wp_query->get( 'photos' );
    if ( $f ) {
        require TEMPLATEPATH . '/content-photos.php';
        exit;
    }
}


function rm_redirect_canonical( $redirect_url, $requested_url ) {    
    global $wp_rewrite;

    // Abort if not using pretty permalinks, is a feed or is not a singular post page
    if ( !$wp_rewrite->using_permalinks() || is_feed() || !is_singular( 'post' ) )
        return $redirect_url;

    // Get the original query parts
    $redirect = @parse_url( $requested_url );
    $original = $redirect_url;
    if ( !isset( $redirect['query'] ) )
        $redirect['query'] = '';

    // If paged append pagination
    if ( get_query_var( 'paged' ) > 0 ) {
        $paged             = (int) get_query_var( 'paged' );
        $redirect['query'] = remove_query_arg( 'paged', $redirect['query'] );

        if ( $paged > 1 )
            $redirect_url .= user_trailingslashit( "page/$paged", 'paged' );
    }

    if ( $redirect_url == $original )
        return $original;

    // tack on additional query vars
    $redirect['query'] = preg_replace( '#^\??&*?#', '',  $redirect['query'] );
    if ( $redirect_url && !empty($redirect['query']) ) {
        parse_str( $redirect['query'], $_parsed_query );
        $_parsed_query = array_map( 'rawurlencode', $_parsed_query );
        $redirect_url  = add_query_arg( $_parsed_query, $redirect_url );
    }

    return $redirect_url;

} 


define( 'SITE_NAME', get_bloginfo( 'name' ) );
define( 'SITE_URL', home_url() );
define( 'THEME_URL', get_template_directory_uri() . '/' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.