如何在 WordPress 中使用永久链接列出自定义分类中的所有项目

发布于 2024-10-11 14:29:22 字数 759 浏览 8 评论 0原文

不知道如何表达这个。我在 WordPress 中设置了一个名为 Products 的自定义帖子类型。我还有两个与 Products 链接的分类法,称为 BrandsCategories。创建产品时,您可以设置产品所属的品牌类别。当使用类型作为永久链接来列出类型中的所有内容时,它工作得很好

http://sitename.com/products

,但我似乎无法让分类法以相同的方式工作,即。列出分类法中的所有项目(我想是档案):

http://sitename.com/brands

http://sitename.com/categories

这可能吗?如果可以,我可能做错了什么?我会尝试在 functions.php 中设置一些重写规则,但如果您想在分类中列出项目,我不知道查询字符串是什么样子,也不知道这是否可能。如果有帮助,我正在使用插件 更多类型Ultimate Taxonomy Manager 创建类型和分类法。

Not sure how to word this. I've set up a custom post type in WordPress called Products. I also have two taxonomies linked to Products called Brands and Categories. When you create a product you can set to which Brand and Category the product belongs. It works perfectly when using the type as permalink to list everything within a type

http://sitename.com/products

but I can't seem to get the taxonomies working in the same way, ie. list all the items within a taxonomy (like an archive, I suppose):

http://sitename.com/brands

http://sitename.com/categories

Is this possible, and if so, what could I possibly be doing wrong? I would try and set up some rewrite rules in functions.php, but I don't know what the query string looks like if you want to list items in a taxonomy, and if this is even possible. If it helps, I'm using the plugins More Types and Ultimate Taxonomy Manager to create the types and taxonomies.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

花伊自在美 2024-10-18 14:29:22

目前还没有真正的方法可以做到这一点。 WP 正在朝这个方向发展,并且在 3.1 中为自定义帖子类型设置了顶层,但仍然没有分类法。这很笨拙,但我添加了一个名为 page-products.php 的页面,然后将我的列表放在那里。

$toplevelterms = get_terms($taxonomyname, 'hide_empty=0&hierarchical=0&parent=0');
$termchildren = array();
foreach ($toplevelterms as $toplevelterm) {
    $termchildren[] = $toplevelterm->term_id;
}
foreach ($termchildren as $child) {
    $term = get_term_by('id', $child, $taxonomyname);
    $write .= '<li>';
    $write .= '<a href="' . get_term_link($term->name, $taxonomyname) . '">' . $term->name . '</a>';
    if ($term->description) $write .= apply_filters('the_content', $term->description);
    $write .= '</li>';
}
if ($write) echo '<ul class="clearfix">' . $write . '</ul>';

There's not really a way to do this yet. WP is moving this direction and with 3.1 is setting up a top-level for custom post types, but still nothing for taxonomies. It's klunky but I added a page called page-products.php and then put my list in there.

$toplevelterms = get_terms($taxonomyname, 'hide_empty=0&hierarchical=0&parent=0');
$termchildren = array();
foreach ($toplevelterms as $toplevelterm) {
    $termchildren[] = $toplevelterm->term_id;
}
foreach ($termchildren as $child) {
    $term = get_term_by('id', $child, $taxonomyname);
    $write .= '<li>';
    $write .= '<a href="' . get_term_link($term->name, $taxonomyname) . '">' . $term->name . '</a>';
    if ($term->description) $write .= apply_filters('the_content', $term->description);
    $write .= '</li>';
}
if ($write) echo '<ul class="clearfix">' . $write . '</ul>';
动次打次papapa 2024-10-18 14:29:22

仅供参考,该插件的正确链接(我是作者)现在是 http://www.get10up.com/plugins/simple-custom-post-type-archives-wordpress/

它确实应该使用 3.1 中的内置存档功能来完成 - 该插件是为3.0。

FYI the correct link for that plugin (I'm the author) is now http://www.get10up.com/plugins/simple-custom-post-type-archives-wordpress/

And it should really be done using the built in archives feature in 3.1 - the plugin was built for 3.0.

却一份温柔 2024-10-18 14:29:22

Trac 票证,但不清楚最明显的事情是什么在此页面上显示:

  1. 附加到该分类法的任何术语的所有帖子的列表,或
  2. 该分类法的所有术语的列表?

WordPress Stack Exchange 上的类似问题 询问选项 2,您的问题可以用两种方式解释:当您说分类中的所有项目时,您是指该分类的所有术语,还是所有与该分类的术语相关的帖子

目前,您可以使用查询变量控制的主查询(循环)将始终返回posts,而不是terms。正如票证中所述,选项 2 需要循环返回此查询的术语,这可能会导致没有模板的主题出现兼容性问题。在此实施之前,大卫的回答可能是正确的选择。

There is a Trac ticket for this, but it's not clear what would the most obvious thing to show on this page:

  1. a list of all posts attached to any term of that taxonomy, or
  2. a list of all terms of this taxonomy?

A similar question on the WordPress Stack Exchange asked for option 2, and your question can be interpreted both ways: when you say all items within a taxonomy, do you mean all terms of this taxonomy, or all posts that are linked to a term of this taxonomy?

Currently the main query (the Loop) that you can control with the query vars will always return posts, not terms. As said in the ticket, option 2 would require the Loop to return terms for this query, possibly causing compatibility issues for themes that don't have a template for it. Until that is implemented, David's answer is probably the way to go.

生活了然无味 2024-10-18 14:29:22

感谢您的回答!我找到了一个完全符合我想要的插件 - 在手动编码所有内容之后:/

http://www.thinkoomph.com/plugins-modules/wordpress-custom-post-type-archives/

Thanks for the answers! I've found a plugin that does exactly what I wanted - after manually coding everything :/

http://www.thinkoomph.com/plugins-modules/wordpress-custom-post-type-archives/

小草泠泠 2024-10-18 14:29:22

使用 WP htaccess 控制插件
http://wordpress.org/extend/plugins/wp-htaccess-control/

在选项中,您可以删除所选分类法的基础。
为我工作,希望它也为你工作:)

Use the WP htaccess Control Plugin
http://wordpress.org/extend/plugins/wp-htaccess-control/

In the options , you can remove the base of taxonomies you select.
Worked for me, hope It works for you as well :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文