首页 文章

Wordpress自定义帖子使用高级永久链接键入无限重定向循环

提问于
浏览
0

使用Wordpress 3.1和最新的高级永久链接和自定义帖子类型UI,我创建了一个名为“人”的自定义帖子类型 . 这个帖子类型的所有孩子的url模式是people / jim,但是当我查看帖子时,我陷入无限重定向循环 . 这只发生在我使用漂亮的永久链接时,而不是在使用id时 .

高级永久链接上使用的永久链接结构是:

常用设置

自定义结构:%postname%

发布

%postname%

  • Wordpress将自定义帖子类型重定向到自身,而不是将其翻译为?people = jim . 我曾尝试在functions.php中自己定义帖子类型,然后进行刷新,但似乎没有像其他人发现的那样解决问题 . 非常感谢任何修复!

1 回答

  • 1

    经过大量的日志记录和调试后,我发现导致无限重定向的函数是 function the_posts($posts) . 如果你注释掉从 if (is_single () && count ($posts) > 0)remove_filter ('the_posts', array (&$this, 'the_posts')); 之前的所有内容,它会停止无限重定向,并且似乎仍能正常运行!唯一的副作用是,如果你去 /jim/ 它会将你重定向回 /people/jim/ (而不仅仅是给你一个404) . 对我来说,这很好,因为它解决了这个问题 .

    所以,再次,在 advanced-permalinks.php 内,搜索 function the_posts ,然后对其进行评论,使其看起来像下面的代码 .

    为什么这会打破它?因为自定义帖子类型is_single并且与任何规则都不匹配,显然,这使得APL将其重新发送回去......无休止地 . 等等 . 也许有一种更聪明的方法可以做到这一点,检查它是否是自定义的帖子类型,或者其他什么,但只是禁用它似乎对我来说没问题 .

    /**
     * Hook that is called when a post is ready to be displayed.  We check if the permalink that generated this post is the
     * correct one.  This prevents people accessing posts on other permalink structures.  A 301 is issued back to the original post
     *
     * @return void
     **/
    
    function the_posts ($posts)
    {
    
    /* DISABLED CODE BELOW:
    
        // Only validate the permalink on single-page posts
        if (is_single () && count ($posts) > 0)
        {
            global $wp, $wp_rewrite;
    
    
            $id = $posts[0]->ID;  // Single page => only one post
    
            // Is this a migrated rule?
            $migrate = get_option ('advanced_permalinks_migration_rule');
    
            if ($migrate)
            {
                if (isset ($migrate[$wp->matched_rule]) && substr (get_permalink ($id), strlen (get_bloginfo ('home'))) != $_SERVER['REQUEST_URI'])
                {
                    wp_redirect (get_permalink ($id));
                    die ();
                }
            }
            else
            {
                // Get the permalink for the post
                $permalink = $this->get_full_permalink ($id);
    
                // Generate rewrite rules for this permalink
                $rules = $wp_rewrite->generate_rewrite_rules ($permalink);
    
                // THIS IS ESPECIALLY PROBLEMATIC PART FOR CUSTOM POSTTYPES
    
                // If the post's permalink structure is not in the rewrite rules then we redirect to the correct URL
    
                if ($wp->matched_rule && !isset ($rules[$wp->matched_rule]))
                {
                    wp_redirect ( get_permalink ($id));
                    die ();
                }
            }
        }
        DISABLED CODE ABOVE */
    
        remove_filter ('the_posts', array (&$this, 'the_posts'));
        return $posts;
    }
    

相关问题