如何处理具有自定义帖子类型的层次结构

时间:2010-10-13 作者:Frederic L

I have moved into WordPress for a few days and was quite impressed by all the available features, plugins and polished themes. I have spent quite some times reading documentation about themes and custom types. So far so good... Now that I\'m trying to do real things, it seems (but I maybe missing something) that WP has some sever limitations.

I want to come up with a classical ISV site. A typical hierarchy would be:

  • Products
  • -- Product A
  • ---- Overview
  • ---- Release History
  • ------- Release 1.5
  • ------- Release 1.4
  • ------- Release 1.3
  • ------- etc.
  • ---- Features
  • ---- etc.
  • -- Product B
  • ---- Overview
  • List item
  • etc...

Focusing on Release History, I came up with:

  • a custom content type: "releases".
  • a custom taxonomy to specify the product associated with a specific "release".
  • I created a template to display the list of "releases".
  • Created a single-release.php to display the detail of a release.
  • Created a page based on my template.

The page URL that display the list of "releases" will basically fit into the hierarchy (/products/product-a/release-history/). However the URL of the custom post type will be something like: /%post_type%/%post_slug%/ (ie: /releases/version 1.2/

I\'m struggling for 2 days trying to find a solution for coming with a coherent URL scheme.

I have tried to use to use Custom post permalinks plugin to come up with an URL that would include my custom taxonomy representing the product (better than nothing even if it\'s not perfect) but for some reason the plugin doesn\'t work on my side.

Overall, unless I\'m missing something, Custom Post Types sound great but are quite a nightmare if you don\'t have a flat structure. I would certainly have preferred a concept of custom content type that is not URL dependant or an easy way to plug a custom post type at any level in the page hierarchy.

That said, I may be completely missing the point. Does anybody have any idea, experience when setting up a site with this kind of hierarchy? Any help appreciated...

[Update]

Not completely sure what are the conventions when updating here but I will leave the initial text for clarity purposes. The purpose of this update is to clarify and add further details.

Products
Created a WP page \'Products\' - no parent - URL: http:///products/

Product A (specific product)
Created a WP page \'Product A\' - parent: \'Products\' - URL: http:///products/product-a/

Overview
Created a WP page \'Overview\' - parent: \'Product A\' - URL: http:///products/product-a/overview/

Releases (list of release for the specific product )
Created a WP page \'What\'s New\' - applied a template that displays a list of all available releases (each release has a link to display the detail of that specific release) - parent: \'Product A\' - URL: http:///products/product-a/product-releases/.
Releases are implemented using a custom type (see below). It was therefore quite easy to implement the template and link to release permalink. Though I haven\'t implemented it yet, my idea was that the template would display the release list for any product based on a taxonomy (product-id) defined at the page level. I could therefore have another page using the same template that would display the releases for another product by having the proper \'product-id\' taxonomy attached to this other page.

Release (detail of a release)
Releases are implemented using a Custom Post Type (let\'s call it \'product-release\'). In addition, I\'m using a custom Taxonomy (product-id) to attach each release to a specific product (obviously, I don\'t want to create another Custom Post Type for the releases related to another product).
Created a \'single-product-release.php\' file to display a specific release. The problem with this approach is that a specific release will be displayed with the following URL: http://<root>/product-releases/release-xx

  • It\'s not really descriptive, the least we can say (I should at least get some product identifier in the url)
  • it somewhat breaks the hierarchy (though one could certainly argue regarding if it\'s wrong or not)

My Options (as far as I can see as a WordPress newbie)

Get rid of the permalink stuff
Maybe I could display the detail of a release using some template and passing some argument in the URL. I could use this template with a page that would go exactly where I want in the hierarchy.
Obviously not an approach I would follow ... for many reasons I won\'t detail here (that\'s already a long post/question).

Find a way to append (or rather prefix) my \'product-id\' value to the Custom Post Type permalink
We could have something like http:///product-a/product-releases/release-x. I would have to accept that releases live somewhat outside of the \'natural\' hierarchy which would be http:///products/product-a/product-releases/release-x.
I could go even further by prefixing with \'products\' - something like /products/%product-id%/%product-release%/. However, I\'m wondering if this Rewrite Rule would not clash with the \'pages\' created under the \'products\' page...

Any input appreciated.

[Update]

I implemented the approach proposed by bobdiaes. This works fine for my content type however this will clash with the WP pages that are mixed in the hierarchy. Using bobdiaes piece of code, I came up with the following Permaling for my product-release custom post type:
/products/%product-id%/product-releases/%product-release%
I will therefore have the proper product URL for displaying the detail of a release for a specific product. ie: /products/my-great-product/product-releases/release-1-5/

However, I want to have some WP pages in the mix:

  • /products/my-great-product/should provide an overview of the product.
  • /products/my-great-product/product-releases/should display the list of releases + the details of the latest release.

At first, it sounds quite natural to create WP pages to do what I need. I will organize my page hierarchy so that the pages fit in the exact URL scheme. However, this approach will clash with the URL rewriting used for the "product-release" Custom Content Type and I won\'t be able to browse the pages (404).

Well, well, well...

So what are my options now

Give up the idea of using WP
Well, I think I\'m going to insist some more. Beside this problem, there are many things to like with WP.

Use Pod CMS
Though I haven\'t tested it yet. It sounds like a nice framework/tool. However, I would loose the ability to easily translate my content using WPML.
The fact that they will be able to use custom post types rather than their own table in version 2.0 will certainly let them rely on current plugins for translation purposes. What they are preparing for version 2.0 sounds interesting. They will be able to use Custom Post Type instead of their own tables.
Pod CMS 2.0 could be THE SOLUTION.

Using WP pages instead of a \'product-release" Custom Post Type
The idea here would be to create a page for each product release (with product-releases page as the parent). I suppose it must be easy to loop through child pages for displaying the list of releases.
Obviously, it has quite some drawbacks:

  • The current admin page interface may not be very practical to handle hundreds of pages...
  • Because the page is already a specialized Custom Post Type (as far as I understand), I would have to remember the specific custom fields I need each time I use a page like a pseudo-content type unless I want to create all fields that may be needed all over the web site. Quite an horrible solution if you ask me.

Replace WP pages by some Custom Post Type
Not sure about the possible rewrite rules clash but here is the idea. Instead of having an overview page, I could come up with a \'product-overview\' Custom Post Type and use a permalink rewriting rule the same way I did for \'product-release\' Custom Post Type. Will update with my findings.

I\'m sure I\'m not the only facing this WordPress URL hell. Though I have to admit that the Generalisation of a "post" into a custom post type is quite clever, the permalink stuff comes with its own constraints.

A word of caution - being new with WP, I may have overlooked something completely obvious

[Update]

Well, as frustrating as it may be, it seems that this is just impossible. Well there is certainly a solution that involves some tricky URL rewriting but certainly nothing trivial for an approach that is however quite common.
Basically, I believe at this stage, that one should tie to a rather simple URL scheme. Will try the wordpress forum, but I don\'t have much hope.

2 个回复
SO网友:joelhaus

在规划更复杂的Wordpress网站时,我会尝试根据默认设置考虑页面/url层次结构WordPress Database Schema. 然后确定最佳URL rewrite 方案和数据结构。一般来说,我发现对用户最重要的数据对象应该作为自定义帖子类型或帖子对象存在,自定义帖子之间的关系应该使用自定义分类法,唯一值应该存储在postmeta中。

More specifically, 如果我提供的产品有版本更新,我可能会为每个产品创建一个自定义帖子类型(假设有版本更新的产品总数有限),并为每个新版本创建一个新帖子(默认情况下,WP按时间顺序存储帖子,因此多个版本应该可以正常工作)。换言之,每个新版本都将作为自定义帖子类型下的单个帖子存在,并且每个新帖子都将包含该版本的详细信息。然后,您可以在自定义帖子类型的页面上显示最近帖子的详细信息。通过强制对每篇文章进行分页,您还应该能够为功能等创建单独的URL,并且您应该能够通过重写URL使它们变得漂亮。

Visually:

/products/%custom-post-type%/
/products/product-a/

/products/%custom-post-type%/archive/
/products/product-a/releases/

/products/%custom-post-type%/%post-name%/
/products/product-a/release-xx/

/products/%custom-post-type%/%post-name%/page/2/
/products/product-a/release-xx/features/

SO网友:bobdiaes

我认为您需要挂接get\\u permalink()函数。我保存在自定义帖子类型注释中的这篇文章可能会有所帮助:http://shibashake.com/wordpress-theme/add-custom-taxonomy-tags-to-your-wordpress-permalinks

结束

相关推荐