WordPress 404,除非url中有空格

时间:2019-07-23 作者:Eujinks

我有一个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;
  }

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

因此,通过聊天讨论后,问题是Ps_Perry_Import_To_Post::wp():

final public static function wp()
{
    ...
    if(!empty($_GET[\'pid\']) && empty($_GET[\'r\'])) {
      ...
      wp_redirect(\'/malta-property/\'.$_GET[\'pid\'].\'/\'.$post_name.\'?r=1\');
      exit();
    }
    if(strpos($_SERVER[\'REQUEST_URI\'], \'/malta-property/\') !== false && empty($_GET[\'r\'])) {
      ...
      if (preg_match(\'/[A-Za-z]/\', $arr2[count($arr2)-1]) && preg_match(\'/[0-9]/\', $arr2[count($arr2)-1])) {
        // do nothing
        return null;
      } else {
        wp_redirect(\'/?pid=\'.$arr1[2]);
        exit();
      }
    }
    return null;
}
它与wp 像这样:

add_action(\'wp\', [\'Ps_Perry_Import_To_Post\', \'wp\'], 9);
因此,如果需要根据$_GET[\'pid\'], 然后尝试以下操作:

final public static function get_prod_by_ref( $wp )
{
    if ( ! empty( $_GET[\'pid\'] ) ) {
        $reference = $_GET[\'pid\'];
        $redirect = true;
    }

    // Check if the request/page path begins with /malta-property/<trans_type>/<location>/<reference>
    // If yes, cancel above redirect and display the single "property" page.
    if ( preg_match( \'#^malta-property/([^/]*)/([^/]*)/([^/]+)#\', $wp->request, $matches ) ) {
        $reference = $matches[3];
        $redirect = false;
    }

    if ( ! empty( $reference ) ) {
        $posts = get_posts( [
            \'post_type\'      => \'property\',
            \'meta_key\'       => \'propertystream_agent_ref\',
            \'meta_value\'     => $reference,
            \'posts_per_page\' => 1,
        ] );

        if ( ! empty( $posts ) ) {
            if ( $redirect ) {
                wp_redirect( get_permalink( $posts[0] ) );
                exit;
            }

            $wp->query_vars[\'name\'] = $posts[0]->post_name;
            $wp->query_vars[\'post_type\'] = \'property\'; // must set post type
        }
    }
}

相关推荐

Altered Media Library URLs

我有一个客户的网站,是在他们离开另一家代理机构后我找到的。该机构使用了一个专有主题和自己的自托管页面生成器,以防止其在除他们之外的任何其他托管环境中更新或编辑。它的另一个方面是重新映射主题的URL并上载目录。因此,例如,代替WP在中查找主题文件http://domain.com/wp-content/themes/…. 它在里面找他们http://domain.com/t/….同样,对于图像上载,也可以在http://domain.com/wp-content/uploads/…, 它在里面找他们http