创建动态URL结构

时间:2013-03-15 作者:WolfieZero

我想为我的WordPress链接和基于页面分类的最终页面创建一个目录结构。

所以我有一个属于city (元)和country (分类学)。我希望最终URL为

http://example.com/{country}/{city}/{post_title}/
这有可能吗?我试过了add_rewrite_rule() 但要么它不能做我想做的,要么我做错了(我去掉了代码,所以我不能显示它,抱歉)。

1 个回复
SO网友:Tom J Nowell

此处演示:

http://thereforei.am/2011/10/28/advanced-taxonomy-queries-with-pretty-urls/

下面是自动生成必要规则的最终代码:

您很可能会在某个阶段希望以编程方式为具有各种分类法的多个自定义帖子类型生成重写规则。你可以手动操作,但那并不有趣。让我们使用自动重写规则生成器。

下面的函数将post类型作为第一个参数,并为其每个分类创建一系列重写规则。该函数还包括一个可选的第二个参数$query\\u vars,它是您可能要为其添加重写规则的任何非分类查询变量的数组。它非常适合为高级元数据查询添加查询变量。例如,我们的事件发布类型可以使用日期查询变量。

/**
 * Generates all the rewrite rules for a given post type.
 *
 * The rewrite rules allow a post type to be filtered by all possible combinations & permutations
 * of taxonomies that apply to the specified post type and additional query_vars specified with
 * the $query_vars parameter.
 *
 * Must be called from a function hooked to the \'generate_rewrite_rules\' action so that the global
 * $wp_rewrite->preg_index function returns the correct value.
 *
 * @param string|object $post_type The post type for which you wish to create the rewrite rules
 * @param array $query_vars optional Non-taxonomy query vars you wish to create rewrite rules for. Rules will be created to capture any single string for the query_var, that is, a rule of the form \'/query_var/(.+)/\'
 *
 * @author Brent Shepherd <[email protected]>
 * @since 1.0
 */
function eg_generate_rewrite_rules( $post_type, $query_vars = array() ) {
    global $wp_rewrite;

    if( ! is_object( $post_type ) )
        $post_type = get_post_type_object( $post_type );

    $new_rewrite_rules = array();

    $taxonomies = get_object_taxonomies( $post_type->name, \'objects\' );

    // Add taxonomy filters to the query vars array
    foreach( $taxonomies as $taxonomy )
        $query_vars[] = $taxonomy->query_var;

    // Loop over all the possible combinations of the query vars
    for( $i = 1; $i <= count( $query_vars );  $i++ ) {

        $new_rewrite_rule =  $post_type->rewrite[\'slug\'] . \'/\';
        $new_query_string = \'index.php?post_type=\' . $post_type->name;

        // Prepend the rewrites & queries
        for( $n = 1; $n <= $i; $n++ ) {
            $new_rewrite_rule .= \'(\' . implode( \'|\', $query_vars ) . \')/(.+?)/\';
            $new_query_string .= \'&\' . $wp_rewrite->preg_index( $n * 2 - 1 ) . \'=\' . $wp_rewrite->preg_index( $n * 2 );
        }

        // Allow paging of filtered post type - WordPress expects \'page\' in the URL but uses \'paged\' in the query string so paging doesn\'t fit into our regex
        $new_paged_rewrite_rule = $new_rewrite_rule . \'page/([0-9]{1,})/\';
        $new_paged_query_string = $new_query_string . \'&paged=\' . $wp_rewrite->preg_index( $i * 2 + 1 );

        // Make the trailing backslash optional
        $new_paged_rewrite_rule = $new_paged_rewrite_rule . \'?$\';
        $new_rewrite_rule = $new_rewrite_rule . \'?$\';

        // Add the new rewrites
        $new_rewrite_rules = array( $new_paged_rewrite_rule => $new_paged_query_string,
                                    $new_rewrite_rule       => $new_query_string )
                             + $new_rewrite_rules;
    }

    return $new_rewrite_rules;
}

结束

相关推荐

hide wp-content from urls

下面是我想从页面源中包含的文件的URL中隐藏wp内容的想法。在wp设置中定义以下内容。phpdefine (\'WP_CONTENT_URL\',\'http://example.com/myownfoldername\'); 并将此添加到.htaccessRewriteRule ^myownfoldername/(.*) /wp-content/$1 [QSA,L] 在localhost上没有其他插件的情况下,这似乎可以很好地工作,但在只有几个插件的在线站点上,它可以无缝地工作。我更