帖子“子页面”的重写规则,带分页

时间:2014-06-13 作者:Marcos Rodrigues

我想知道是否可以在帖子中添加“子页面”,并在该“子页面”中对内容进行分页,例如:

我有一个帖子Lorem Ipsum
此帖子Videos, PhotosAudios 当您访问帖子时作为附件www.website.com/lorem-ipsum/ 您基本上只访问文本和一些其他信息,您可以访问:

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我的功能中有这个。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;
        }
    }
    
    ?>
    
    有了这些,我就有了以下URL“工作”:

    www.website。com/lorem ipsum/photos/

  • www.website。com/lorem ipsum/photos/page/2但分页不起作用,它看起来像分页的var它没有通过,它总是显示第一页的内容,无论您在地址栏中键入什么页面

    /photos Request
    照片页面上的请求(www.website.com/lorem ipsum/photos/):

    Request:

    Query String:
    name=lorem ipsum dolor sit amet&;照片=1

    Matched Rewrite Rule:
    ([^/]+)(/[0-9]+)/照片/$

    Matched Rewrite Query:
    name=lorem ipsum dolor sit amet&;照片=1

    /photos/page/2 Request
    照片页面上的请求(www.website.com/lorem ipsum/photos/page/2):

    Request:

    Query String:
    name=lorem ipsum dolor sit amet&;照片=1

    Matched Rewrite Rule:
    ([^/]+)(/[0-9]+)/照片/页码/?([0-9]{1,})/$

    Matched Rewrite Query:
    name=lorem ipsum dolor sit amet&;分页=&;照片=1

    谢谢

  • 1 个回复
    最合适的回答,由SO网友:Marcos Rodrigues 整理而成

    在对

    我被介绍给Redirect Canonical

    根据站点URL将传入链接重定向到正确的URL。

    搜索引擎考虑www.somedomain。com和somedomain。com是两个不同的URL,当它们都指向同一位置时。此SEO增强通过将所有传入链接重定向到其中一个来防止对重复内容的惩罚。

    正因为如此,所有分页的页面都被重定向到了域。com/lorem=ipsum/photos当我想要域名时。com/lorem ipsum/photos/page/2

    因此,我需要改变redirect_canonical 使用过滤器redirect_canonical

    PS: 如果你写了一个好的重写规则,但它会把你带到另一个页面,那么它可以重定向\\u规范地按照你的重写规则操作

    我希望这对其他人有帮助,抱歉英语不好。

    你可以检查我的最终功能。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() . \'/\' );
    

    结束

    相关推荐