最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - Regex with a possible hyphen

programmeradmin5浏览0评论

I am working on a site for cars. I wrote a regex which worked, up until the point where brands are separated by a hyphen.

These are 2 examples I want to match:

  • class-s/ford-shelby-gt350r
  • class-s/exotic-rides-w70

The regex I used for it is

([a-z-]+)\/([a-z]+[-]*[a-z]+)-([a-zA-Z0-9]+[-]*[a-z0-9]+)\/?$

But this matches Shelby as the brand and not as type.

This is how I use it inside Wordpress

$wp_rewrite->add_permastruct( 'cars', 'cars/%class%/%brand%-%postname%/', false );
add_rewrite_rule( 'cars/([a-zA-Z-]+)/([a-zA-Z]+)-([a-zA-Z0-9-]+)/?$', 'index.php?post_type=as_car&name=$matches[3]', 'top' );

I think I need to check (with a lookup) if the brand contains a hyphen, but I don't know how to do it.

Can someone help me do it ?

This is a link to my regex editor:

ADDED: I changed my code. I now generate a full post_name, so I don't have to regex anything...

发布评论

评论列表(0)

  1. 暂无评论