我有一个previous question 回答后,我尝试修改解决方案,但现在的问题是,当我的自定义帖子URL中没有空格时,标签位于/location/ (英寸domain.com/malta-property/department/location/reference_id) - 原来只是/malta-property/location/reference_id) 但现在我添加了/department/ 那么位置就会404 除非名称中有空格。这很奇怪。注:trans_type 指部门
可能有意思的地方是正则表达式?但我看不出它们有什么错:只是为了特殊性,regex片段是:
  $wp_rewrite->add_rewrite_tag(\'%reference%\', \'([^/]+)\', \'reference=\');
  $wp_rewrite->add_rewrite_tag(\'%trans_type%\', \'([^/]+)\', \'trans_type=\');
  $wp_rewrite->add_rewrite_tag(\'%location%\', \'([^/]+)\', \'location=\');
 以及
if ( preg_match( \'#^malta-property/([^/]+)/([^/]+)/([^/]+)#\', $wp->request, $matches ) ) {
 为属性构建自定义url的代码如下:
(注册表桩类型参数):
final private static function register_post_types ()
{
    $labels = array(
        \'name\'               => _x( \'Properties\', \'post type general name\', \' propertystreambootstrap\' ),
        \'singular_name\'      => _x( \'Property\', \'post type singular name\', \' propertystreambootstrap\' ),
        \'menu_name\'          => _x( \'Properties\', \'admin menu\', \' propertystreambootstrap\' ),
        \'name_admin_bar\'     => _x( \'Property\', \'add new on admin bar\', \' propertystreambootstrap\' ),
        \'add_new\'            => _x( \'Add New\', \'property\', \' propertystreambootstrap\' ),
        \'add_new_item\'       => __( \'Add New Property\', \' propertystreambootstrap\' ),
        \'new_item\'           => __( \'New Property\', \' propertystreambootstrap\' ),
        \'edit_item\'          => __( \'Edit Property\', \' propertystreambootstrap\' ),
        \'view_item\'          => __( \'View Property\', \' propertystreambootstrap\' ),
        \'all_items\'          => __( \'All Properties\', \' propertystreambootstrap\' ),
        \'search_items\'       => __( \'Search Properties\', \' propertystreambootstrap\' ),
        \'parent_item_colon\'  => __( \'Parent Properties:\', \' propertystreambootstrap\' ),
        \'not_found\'          => __( \'No properties found.\', \' propertystreambootstrap\' ),
        \'not_found_in_trash\' => __( \'No properties found in Trash.\', \' propertystreambootstrap\' )
    );
    $args = array(
        \'labels\'             => $labels,
        \'description\'        => __( \'Properties for your website.\', \' propertystreambootstrap\' ),
        \'public\'             => true,
        \'publicly_queryable\' => true,
        \'show_ui\'            => true,
        \'show_in_menu\'       => true,
        \'menu_icon\'          => \'dashicons-building\',
        \'query_var\'          => true,
        \'rewrite\'            => array( \'slug\' => \'malta-property/%trans_type%/%location%/%reference%\' ),
    //\'rewrite\'            => array( \'slug\' => \'malta-property\'),
    \'has_archive\' => \'about-cool-post-types\',
        \'capability_type\'    => \'post\',
        \'has_archive\'        => false,
        \'hierarchical\'       => false,
        \'menu_position\'      => null,
        \'supports\'           => array(\'title\', \'editor\', \'excerpt\', \'post-thumbnails\', \'thumbnail\')
    );
    register_post_type( \'property\', $args );
 调用的url重写
init 我冲洗过permalinks:
final public static function url_rewrite()
{
    global $wp_rewrite;
    $permastruct = $wp_rewrite->extra_permastructs[\'property\'];
    $permastruct[\'struct\'] = str_replace( \'/%property%\', \'\', $permastruct[\'struct\'] );
    $wp_rewrite->extra_permastructs[\'property\'] = $permastruct;
    $wp_rewrite->add_rewrite_tag(\'%reference%\', \'([^/]+)\', \'reference=\');
    $wp_rewrite->add_rewrite_tag(\'%trans_type%\', \'([^/]+)\', \'trans_type=\');
    $wp_rewrite->add_rewrite_tag(\'%location%\', \'([^/]+)\', \'location=\');
    return null;
}
 通过URL获取产品:
final public static function get_prod_by_ref( $wp )
{
    // Check if the request/page path begins with /malta-property/<trans_type>/<location>/<reference>
    if ( preg_match( \'#^malta-property/([^/]+)/([^/]+)/([^/]+)#\', $wp->request, $matches ) ) {
        $posts = get_posts( [
            \'post_type\'      => \'property\',
            \'meta_key\'       => \'propertystream_agent_ref\',
            \'meta_value\'     => $matches[3],
            \'posts_per_page\' => 1,
        ] );
        if ( ! empty( $posts ) ) {
            $wp->query_vars[\'name\'] = $posts[0]->post_name;
            $wp->query_vars[\'post_type\'] = \'property\'; // must set post type
        }
    }
}
 构建自定义permalink结构(过滤为
post_type_link):
final public static function permalink_structure ($permalink, $post, $leavename)
{
    if (false !== strpos($permalink, \'%reference%\') && false !== strpos($permalink, \'%location%\') && false !== strpos($permalink, \'%trans_type%\')) {
      $reference = get_post_meta($post->ID, \'propertystream_agent_ref\', true);
      $location = wp_get_post_terms($post->ID, \'location\');
      $type = wp_get_post_terms($post->ID, \'trans_type_id\');
      //check trans_type_id is set, it it is and is resale the slug should be sales. otherwise just assign it the value
      if (!empty($type)) {
        if ($type[0]->slug == \'resale\') {
          $type = \'sales\';
        }
        else
        {
          $type = $type[0]->slug;
        }
      }
      else {
        $type = null;
      }
      $location = !empty($location) ? $location[0]->name : null;
      $rewritecode = array(
        \'%reference%\',
        \'%location%\',
        \'%trans_type%\'
      );
      $rewritereplace = array(
        $reference,
        $location,
        $type,
      );
      $permalink = str_replace($rewritecode, $rewritereplace, $permalink);
    }
    return $permalink;
  }