从分层自定义帖子类型中删除基本插件

时间:2013-09-18 作者:Jonathan Wold

我有一个称为“状态”的分层自定义帖子类型。我的目标是建立一个这样的永久链接结构:

http://website.com/idaho/http://website.com/idaho/sub-page

我差一点了,但还没有完全弄明白。下面是我使用的代码:

First, I remove the slug

public function remove_slugs( $permalink, $post, $leavename ) { 

      $url_components = parse_url( $permalink );
      $post_path = $url_components[\'path\'];
      $post_name = end( explode( \'/\', trim( $post_path, \'/\' ) ) );

      if( !empty( $post_name )) {

        switch($post->post_type) {

          case \'state\':

            if( $post->post_parent ) {

                $parent = get_post( $post->post_parent );
                $parent = $parent->post_name;

                $permalink = str_replace( $post_path, \'/\' . $parent . \'/\' . $post_name . \'/\', $permalink );

            } else { 

                $permalink = str_replace( $post_path, \'/\' . $post_name . \'/\', $permalink );
            }

            break;

        }

      }

      return $permalink;

    }
这是连接到post_type_link.

Next, I reset the query variables so WordPress knows we\'re dealing with a CPT

    public function parse_custom_post_type( $query ) {

        if ( ! $query->is_main_query() ) return;
        if ( count( $query->query ) != 2 || ! isset( $query->query[\'page\'] ) ) return;

        // Are we dealing with a page?
        if ( ! empty( $query->query[\'pagename\'] ) && ! is_home() ) {

            // If the page doesn\'t exist, we must be dealing with a state
            if ( ! is_page( $query->query[\'pagename\'] ) ) {
                $query->set( \'name\', $query->query[\'pagename\'] );
                $query->set( \'state\', $query->query[\'pagename\'] );
                $query->set( \'post_type\', \'state\' );
                $query->is_page = 0;
                $query->is_single = 1;
                unset( $query->query_vars[\'page\'] );
                unset( $query->query_vars[\'pagename\'] );
                unset( $query->query[\'page\'] );
                unset( $query->query[\'pagename\'] );
            }
        }

    }
这是挂在里面的pre_get_posts.

因此,一级页面有效,但子页面无效。URL解析,然后点击404。

我需要做什么才能让它工作?

1 个回复
最合适的回答,由SO网友:Matthew Boynes 整理而成

在我开始之前有两件事要做:

你真的不应该这样做。为了提高效率,可以使用静态锚来重写规则。你最好的选择是找到一个你满意的slug——也许可以用“活动”代替“状态”(或者不管你的子页面是什么)Here are instructions for setting up rewrites for the former 从我在波特兰的WordCamp演讲中here are instructions for the latter 来自此网站上的另一个问题

    代码

    如果您决定继续使用此结构,那么如果您的帖子类型的“hierarchical”设置为true,“rewrite”设置为false,那么这里有一个单件类应该可以为您提供。在将其添加到主题函数后,请确保刷新重写。php文件或您的插件(转到Settings→Permalinks并单击“保存更改”)。

    /**
     * Strip the slug out of a hierarchical custom post type
     */
    
    if ( !class_exists( \'State_Rewrites\' ) ) :
    
    class State_Rewrites {
    
        private static $instance;
    
        public $rules;
    
        private function __construct() {
            /* Don\'t do anything, needs to be initialized via instance() method */
        }
    
        public static function instance() {
            if ( ! isset( self::$instance ) ) {
                self::$instance = new State_Rewrites;
                self::$instance->setup();
            }
            return self::$instance;
        }
    
        public function setup() {
            add_action( \'init\',                array( $this, \'add_rewrites\' ),            20 );
            add_filter( \'request\',             array( $this, \'check_rewrite_conflicts\' )     );
            add_filter( \'state_rewrite_rules\', array( $this, \'strip_state_rules\' )           );
            add_filter( \'rewrite_rules_array\', array( $this, \'inject_state_rules\' )          );
        }
    
        public function add_rewrites() {
            add_rewrite_tag( "%state%", \'(.+?)\', "state=" );
            add_permastruct( \'state\', "%state%", array(
                \'ep_mask\' => EP_PERMALINK
            ) );
        }
    
        public function check_rewrite_conflicts( $qv ) {
            if ( isset( $qv[\'state\'] ) ) {
                if ( get_page_by_path( $qv[\'state\'] ) ) {
                    $qv = array( \'pagename\' => $qv[\'state\'] );
                }
            }
            return $qv;
        }
    
        public function strip_state_rules( $rules ) {
            $this->rules = $rules;
            # We no longer need the attachment rules, so strip them out
            foreach ( $this->rules as $regex => $value ) {
                if ( strpos( $value, \'attachment\' ) )
                    unset( $this->rules[ $regex ] );
            }
            return array();
        }
    
        public function inject_state_rules( $rules ) {
            # This is the first \'page\' rule
            $offset = array_search( \'(.?.+?)/trackback/?$\', array_keys( $rules ) );
            $page_rules = array_slice( $rules, $offset, null, true );
            $other_rules = array_slice( $rules, 0, $offset, true );
            return array_merge( $other_rules, $this->rules, $page_rules );
        }
    }
    
    State_Rewrites::instance();
    
    endif;
    
    我觉得这个话题很有意思,我写了一篇深刻的解释on my blog. 在这里,我包含了一些稍微复杂一些的代码,这些代码与您的特定问题不太相关。

结束

相关推荐