如何指定自定义帖子和分类的URL

时间:2013-01-17 作者:Tom Harmer

我读过一些关于这方面的帖子,但找不到明确的答案:(

在自定义帖子中,我有一个名为{ondemand}的自定义帖子类型,我有三个分类法{speakers}和{categories},还有{season},每个分类法都有3-4个术语,例如在{speakers}下有一个名为[joebloggs]目前我在我们的网站上有一个主URL,最新的帖子位于:www.mysite。com/ondemand命令

当我按分类术语筛选帖子时,URL默认为:www.mysite。com/ondemand/{speakers}/[joebloggs](使用taxonomy.php显示结果)

然而,我希望任何过滤过的术语的URL都是这样的:www.mysite。com/ondemand/[joebloggs]

这是我在函数中使用的代码。php-我尝试在每个分类法中重写(被注释掉),但没有成功-它们导致404页。。。。如有任何建议或重新编码提示,将不胜感激

/**
* On Demand TV Post Type
*/

add_action(\'init\', \'ondemand\');

function ondemand() {

$labels = array(
\'name\' => __(\'On Demand TV\', \'post type general name\'),
\'singular_name\' => __(\'TV Episode\', \'post type singular name\'),
\'add_new\' => __(\'New TV Episode\', \'TV Episode\'),
\'add_new_item\' => __(\'Add TV Episode\'),
\'edit_item\' => __(\'Edit TV Episode Item\'),
\'new_item\' => __(\'New TV Episode Item\'),
\'view_item\' => __(\'View TV Episode Item\'),
\'search_items\' => __(\'Search TV Episode\'),
\'not_found\' => __(\'Nothing found\'),
\'not_found_in_trash\' => __(\'Nothing found in Trash\'),
\'parent_item_colon\' => \'\'
);

$args = array(
\'labels\' => $labels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'query_var\' => true,
\'rewrite\' => false,    
\'capability_type\' => \'post\',
\'hierarchical\' => true,
\'menu_position\' => 3,   
\'supports\' => array(\'title\',\'editor\',\'comments\',\'thumbnail\')
); 

register_post_type( \'ondemand\' , $args );

register_taxonomy("Categories", array("ondemand"), 
array("hierarchical" => true, 
"label" => "Category", 
"singular_label" => "Category", 
"rewrite" => true));

register_taxonomy("Speaker", array("ondemand"), 
array("hierarchical" => true, 
"label" => "Speakers", 
"singular_label" => "Speaker", 
"rewrite" => true));
//"rewrite" => array(\'slug\' =>\'ondemand\', \'hierarchical\' => true, \'with_front\' => false)));

register_taxonomy("Season", array("ondemand"), 
array("hierarchical" => true, 
"label" => "Seasons", 
"singular_label" => "Season",
"rewrite" => true));
//"rewrite" => array(\'slug\' =>\'ondemand\', \'hierarchical\' => true, \'with_front\' => true)));

}

1 个回复
SO网友:Seamus Leahy

我以前做过,但是I warn you that this is something to avoid doing. 当您的术语数量非常有限且不变时,这是最好的。

警告结束后,下面是如何做到这一点。您需要创建一个包含术语slug的重写规则,以避免违反重写规则。如果您使用了“包罗万象”,那么它将打破页面的重写规则。

function my_custom_rewrite_rules_array( $rules ) {
    global $wp_rewrite;

    // Generate the regexp for the terms
    $terms = get_terms( \'Speaker\', array( \'hide_empty\' => false ) );
    $term_slugs = array();
    foreach ( $terms as $term ) {
        $term_slugs[] = preg_quote( $term->slug );
    }

    // (term1-slug|term2-slug|term3-slug)
    $terms_regexp = \'(\' . implode( \'|\', $term_slugs ) . \')\';

    // add_rewrite_tag( $tagname, $regex, $query )
    $wp_rewrite->add_rewrite_tag( "%speaker%", $terms_regexp, "taxonomy=speaker&term=" );
    // Use generate_rewrite_rules to make the various rules for pagination and other endpoints
    $speaker_rules = $wp_rewrite->generate_rewrite_rules( \'/%speaker%/\' );

    // Add our speaker rules
    return $speaker_rules + $rules;
}
add_filter( \'rewrite_rules_array\', \'my_custom_rewrite_rules_array\' );
正如您所看到的,这样做的缺点是每次创建新术语时都需要更新重写规则。无论何时发生变化,我们都可以重建规则。

add_action( \'created_term\', \'my_custom_flush_rewrite_rules\', 10, 3 );
add_action( \'edited_term\', \'my_custom_flush_rewrite_rules\', 10, 3 );
add_action( \'delete_term\', \'my_custom_flush_rewrite_rules\', 10, 3 );
function my_custom_flush_rewrite_rules( $term_id, $tt_id, $taxonomy ) {
    $taxonomies_to_refresh = array( \'Speaker\' );
    if ( in_array( $taxonomy, $taxonomies_to_refresh ) ) {
        flush_rewrite_rules();
    }
}

结束

相关推荐

widgetlogic and permalinks

我试图使用widgetlogic在某些页面上有条件地显示菜单。每个菜单都使用如下标记is_page(array(\"Page Name\", \"Page Name 2\" ...)), 在我尝试更改permalinks之前,它一直工作得很好(因此所有菜单都会从各自的页面中消失)。我做错什么了吗?是否有解决方法?