我有一个称为“状态”的分层自定义帖子类型。我的目标是建立一个这样的永久链接结构:
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。
我需要做什么才能让它工作?