【问题标题】:wordpress url rewriting for custom template自定义模板的wordpress url重写
【发布时间】:2012-11-08 13:31:54
【问题描述】:

我对 wordpress 非常陌生,对如何为 wordpress 自定义模板创建自定义 url 重写感到困惑。我也尝试在谷歌上找到它,但没有运气。我找不到合适的解决方案。

这是我当前的 url 结构:

http://example.com/chiptuning/search/?brand=Audi

使用自定义 url 重写我想将其转换为

http://example.com/chiptuning/search/brand/audi

也可能是

http://example.com/chiptuning/search/Audi

还请说明代码的放置位置。

更新

@Mike Lewis 提供的答案已经被我使用,但它没有提供我正在寻找的东西,无论如何感谢@Mike Lewis 的回复。

其他人对这个问题有什么建议吗?我需要它,请快点。

提前感谢。

【问题讨论】:

    标签: wordpress url-rewriting


    【解决方案1】:

    WordPress 在查找要使用的模板文件方面发挥了自己的作用。如果你知道重写规则,你可以看看这些页面:

    或者,这里有一些更简单的解决方案:

    使用分层页面

    site.com/page1/sub-page/sub-page/

    您必须使用浏览器通过 /wp-admin/ 添加页面,然后您可以在其中设置其父页面。您还可以选择要使用的特定模板(PHP 主题文件)(请参阅:Theme Development 并向下滚动到自定义页面模板)。

    使用分层类别

    您还可以使用分层类别来标记帖子,然后在类别存档页面上列出它们。例如:

    您可以拥有 site.com/category/brand/audi/(请注意,您可以在 wordpress 设置中更改此网址的“类别”基本部分)。

    您可以为各种标签存档页面创建模板文件:category-brand.php 将为site.com/category/brand/ url 加载,category-audi.php 将为site.com/category/brand/audi/ url 加载。

    注意:根据您的网站需要的动态程度,您可能需要跳过使用 category-{tag}.php,这样它就可以使用通用 category.php。在这种情况下,我相信上述两个 url 都会使用通用 category.php(在没有更具体的模板文件的情况下 - Template Hierarchy 向下滚动以查看模板层次结构图)。

    这将使您不必为每个单独的标签创建单独的文件。使用 get_query_var() 方法获取具体的分类标签。

    最后但并非最不重要的一点

    如果您仍然感到困惑,还有另一种方法可以对 url 进行切片和切块,而无需使用重写规则。 BuddyPress(wordpress 的插件)有一个功能,可以将 url 分成几部分(/one/two/three/),然后在 PHP 中使用它们。我不确定它到底是如何工作的,但如果你想看看,你可以谷歌/下载 BuddyPress,然后查看 buddypress/bp-core/bp-core-catchuri.php 文件。

    这是文档块:

    /**
     * Analyzes the URI structure and breaks it down into parts for use in code.
     * BuddyPress can use complete custom friendly URI's without the user having to
     * add new re-write rules. Custom components are able to use their own custom
     * URI structures with very little work.
     *
     * @package BuddyPress Core
     * @since BuddyPress (1.0)
     *
     * The URI's are broken down as follows:
     *   - http:// domain.com / members / andy / [current_component] / [current_action] / [action_variables] / [action_variables] / ...
     *   - OUTSIDE ROOT: http:// domain.com / sites / buddypress / members / andy / [current_component] / [current_action] / [action_variables] / [action_variables] / ...
     *
     *  Example:
     *    - http://domain.com/members/andy/profile/edit/group/5/
     *    - $bp->current_component: string 'xprofile'
     *    - $bp->current_action: string 'edit'
     *    - $bp->action_variables: array ['group', 5]
     *
     */
    function bp_core_set_uri_globals() {
    

    【讨论】:

    • 这个插件已经被我使用了,但没有运气 :( 你有什么其他的想法或其他建议吗?请我快点 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-19
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多