每次重写按章节分开发布帖子

时间:2020-04-09 作者:Công Tử Huyết

我有一个尚未解决的问题,请帮助我。我使用Wordpress。

我的URLindex.php?story=slug_story&chapter=id_parent-slug_chapter

我的代码

add_action( \'init\', function () {
    add_rewrite_rule(
        \'^name/([^/]+)/([^/]+)/?$\',
        \'index.php?story=$matches[1]&chapter=$matches[2]\',
        \'top\'
    );

}, 1, 1 );
我的结果name/slug_story/id_parent-slug_chapter => 为我工作。

但是如何重写URL=>name/slug_story/slug_chapter ?

我使用post_parent 在里面wp_posts 连接post\\U类型story vs公司chapter. 尝试了您的代码@simongcc,但不起作用。

add_filter( \'request\', \'q363618_modify_request_query\' ) ;
function q363618_modify_request_query( $query ) {
    global $wp_rewrite, $post;

    if (   ! isset( $wp_rewrite ) 
        || ! is_object( $wp_rewrite ) 
        || ! $wp_rewrite->using_permalinks() 
        || ! isset( $_SERVER[\'REQUEST_URI\'] ) 
    ) {
        return;
    }

    if ( $post->post_type == \'chapter\' ){
        if ( empty(get_post($post->post_parent)->post_name))  {

        } else {
            $parentName = get_post($post->post_parent)->post_name;
            $slug = $post->post_name;
            // preg_match(\'#[0-9]-(.*)#\', $slug, $slug);
            // $slug = $slug[1];
            $query[\'chapter\'] = $slug;

        }
    }
    return $query; // after that, WordPress will think slug_chapter = id_parent-slug_chapter
}

add_action( \'template_redirect\', \'q363618_redirect_to_permalink\' );
function q363618_redirect_to_permalink() {
    global $wp_rewrite, $wp, $post;

    if (   ! isset( $wp_rewrite ) 
        || ! is_object( $wp_rewrite ) 
        || ! $wp_rewrite->using_permalinks() 
        || ! isset( $_SERVER[\'REQUEST_URI\'] ) 
    ) {
        return;
    }

    if( ! empty( $wp->query_vars[\'chapter\'] ) ) { 

        if ( $post->post_type == \'chapter\' ){
            if ( empty(get_post($post->post_parent)->post_name))  {

            } else {
                $parentName = get_post($post->post_parent)->post_name;
                $slug = $post->post_name;
                preg_match(\'#[0-9]-(.*)#\', $slug, $slug);
                $slug = $slug[1];

                $redirect = home_url( \'name/\'.$parentName.\'/\' . $slug );
                wp_redirect( $redirect );    
            }
        }

    } else {
        return;
    }
}
EESULT返回404 not found,我在wp admin中更新了permalink

1 个回复
SO网友:simongcc

因为重写主题需要繁琐的测试和时间。这不是一个答案-一个密码

因为它涉及到来自询问者的许多未知因素,例如帖子类型是如何创建的,查询变量中影响查询参数的设置是否涉及任何插件,如页面结构等其他编码或设置,所以通常如果询问者可以提供更多信息,会有所帮助。

为了不同的观众,我根据@TomJ Nowell的建议在这里添加了更多的注释。以下建议假设您

熟悉php的人很容易跟踪和调整WordPress源文件,可能需要进一步的调试过程,耐心阅读和测试请允许我复制asker代码并重构描述以获得更好的解释。

我以为它在哪里工作storychapter 在WordPress中,开发人员已在代码中的某个位置设置了允许的。

add_action( \'init\', function () {
    add_rewrite_rule(
        \'^name/([^/]+)/([^/]+)/?$\',
        \'index.php?story=$matches[1]&chapter=$matches[2]\',
        \'top\'
    );

}, 1, 1 );
此代码表示解释

http//example.com/name/slug_story/id_parent-slug_chapter

index.php?story=slug_story&chapter=id_parent-slug_chapter

所以它会解释

http//example.com/name/slug_story/slug_chapter

index.php?story=slug_story&chapter=slug_chapter

在WordPress重写系统中,

它解析URL,将其转换为查询参数,并且总是首先要做的事情。这就是为什么简单的重定向不起作用chapter=slug_chapter 但它并不存在。因为真名是id_parent-slug_chapter.

所以要想让它发挥作用,你需要告诉WordPress

slug_chapter = id_parent-slug_chapter

理解后,以下是我成功完成的可能解决方案。2个步骤

告诉WordPressslug_chapter 等于id_parent-slug_chapterid_parent-slug_chapter 到slug_chapter

* But here, I assumed that other query parameters that WordPress received are same for both link other than chapter. If there is any other difference. You may have to read further for debugging a 404 result.

因为我不确定询问者的帖子名称背后的逻辑。魔法检查留给询问者。

// Part (A)
// This filter means to modify the query before it pass for matching a template
// so this is useful for customisation

add_filter( \'request\', \'q363618_modify_request_query\' ) );
function q363618_modify_request_query( $query ) {
    global $wp_rewrite;

    // no need to run if permalink is not being used
    if (   ! isset( $wp_rewrite ) 
        || ! is_object( $wp_rewrite ) 
        || ! $wp_rewrite->using_permalinks() 
        || ! isset( $_SERVER[\'REQUEST_URI\'] ) 
        // add more checking if you need
    ) {
        return;
    }

    // add your magic here
    // after the chapter name, eg. you may have a way to compare and transform them
    // tell WordPress slug_chapter = id_parent-slug_chapter

    $query[\'chapter\'] = \'id_parent-slug_chapter\'; // you may put some static value to test if this filter works for you, if it works, together with the following redirect filter, it will redirect to where you expect

    // eg. try to use a static value like $query[\'chapter\'] = \'14-story1\' to see if it takes effect;

    return $query; // after that, WordPress will think slug_chapter = id_parent-slug_chapter
}
WordPress识别chapter 第(A)部分。这很可能会奏效。(因为还有一些我可能没有考虑到的具体情况,所以我不能保证它会立即生效)

// Part (B)
// This only do redirect, redirect itself is 100% working if the logic and conditions is right. If Part (A) fail, it will still redirect to 404.

add_action( \'template_redirect\', \'q363618_redirect_to_permalink\' );
function q363618_redirect_to_permalink() {
    global $wp_rewrite;

    if (   ! isset( $wp_rewrite ) 
        || ! is_object( $wp_rewrite ) 
        || ! $wp_rewrite->using_permalinks() 
        || ! isset( $_SERVER[\'REQUEST_URI\'] ) 
    ) {
        return;
    }

    // check query
    if( ! empty( $wp->query_vars[\'chapter\'] ) ) { // example only, you can add required conditions

        // check url
        // if found any pattern `/name/slug_story/id_parent-slug_chapter`
        // change it to `/name/slug_story/slug_chapter`
        // this part redirect url only, part (A) is important that it tell WordPress what template to match

        $redirect = home_url( \'/name/slug_story/slug_chapter\' );

        wp_redirect( $redirect );
    } else {
        // do not affect other pages
        return;
    }
}
404跟踪如果仍要跟踪404,则需要跟踪原因。由于情况很多,因此涉及不同的查询参数。跟踪可以确保缺少哪个部分。然后你只需填补缺失的部分,让它发挥作用。

询问者的检查逻辑和注释中解释的问题。

add_filter( \'request\', \'q363618_modify_request_query\' ) ;
function q363618_modify_request_query( $query ) {
    global $wp_rewrite, $post; // <=== it is right, however

    // at this point of loading, $post is not setup
    // so it is always null

    if (   ! isset( $wp_rewrite ) 
        || ! is_object( $wp_rewrite ) 
        || ! $wp_rewrite->using_permalinks() 
        || ! isset( $_SERVER[\'REQUEST_URI\'] ) 
    ) {
        return;
    }

    // this checking will never got to work
    // at this moment, only can use $query, modify $query and then setup $post
    // eg. $query[\'post_type]\', $query[\'chapter]\' or whatever available
    // in php 7.4.3, it will display error rather than false

        if ( $post->post_type == \'chapter\' ){
        if ( empty(get_post($post->post_parent)->post_name))  {

        } else {
            $parentName = get_post($post->post_parent)->post_name;
            $slug = $post->post_name;
            // preg_match(\'#[0-9]-(.*)#\', $slug, $slug);
            // $slug = $slug[1];
            $query[\'chapter\'] = $slug;

        }
    }


    return $query;
}

它是由几个地方设置的,简单来说就是如果$wp\\u query->posts return false,或者如果查询参数无效或与某些内容不匹配

快速简单地找出原因:

http//example.com/name/slug_story/id_parent-slug_chapter

http//example.com/name/slug_story/slug_chapter

add_filter( \'request\', \'q363618_trace_request_query\' );
function q363618_trace_request_query( $query ) {
    global $wp_rewrite;

    var_dump($query);
    exit(); // yes, terminate the php continue to work and see the $query at this point
    return $query; // after that, WordPress will think slug_chapter = id_parent-slug_chapter
}
比较两个查询以查看遗留的内容,然后修改逻辑和条件,以提供预期的和有效的$查询

如果您可以创建相同的查询,那么结果应该是相同的,重定向将起作用。

相关推荐