如何修复具有分层分类的自定义固定链接中的双斜杠?

时间:2012-12-06 作者:jochem

遵循Jeff@Custom permalinks - post type - hierarchical taxonomy's

我成功地为自定义分类法重写了url。然而,有一件事让我感到困扰,那就是由于分隔符,输出中出现了双斜杠。

http://www.domain.nl/product/televisies/led/55-inch//product-naam-4/

我无法在de admin的permalinks部分更改此内容。(/%类别%/%postname%/)

register_post_type( "products", 
            array(  \'label\'             => CUSTOM_MENU_TITLE,
                    \'labels\'            => array(   \'name\'                  =>  CUSTOM_MENU_NAME,
                                                    \'singular_name\'         =>  CUSTOM_MENU_SIGULAR_NAME,
                                                    \'add_new\'               =>  CUSTOM_MENU_ADD_NEW,
                                                    \'add_new_item\'          =>  CUSTOM_MENU_ADD_NEW_ITEM,
                                                    \'edit\'                  =>  CUSTOM_MENU_EDIT,
                                                    \'edit_item\'             =>  CUSTOM_MENU_EDIT_ITEM,
                                                    \'new_item\'              =>  CUSTOM_MENU_NEW,
                                                    \'view_item\'             =>  CUSTOM_MENU_VIEW,
                                                    \'search_items\'          =>  CUSTOM_MENU_SEARCH,
                                                    \'not_found\'             =>  CUSTOM_MENU_NOT_FOUND,
                                                    \'not_found_in_trash\'    =>  CUSTOM_MENU_NOT_FOUND_TRASH ),
                    \'public\'            => true,
                    \'can_export\'        => true,
                    \'show_ui\'           => true, // UI in admin panel
                    \'_builtin\'          => false, // It\'s a custom post type, not built in
                    \'_edit_link\'        => \'post.php?post=%d\',
                    \'capability_type\'   => \'post\',
                    \'menu_icon\'         => get_bloginfo(\'template_url\').\'/images/favicon.ico\',
                    \'hierarchical\'      => true,
                    \'rewrite\'           => array(\'slug\' => \'product/%taxonomy_name%\',\'with_front\' => true,\'hierarchical\'=>true), // Permalinks
                    \'query_var\'         => "products", // This goes to the WP_Query schema
                    \'supports\'          => array(   \'title\',
                                                    \'author\', 
                                                    \'excerpt\',
                                                    \'thumbnail\',
                                                    \'comments\',
                                                    \'editor\', 
                                                    \'trackbacks\',
                                                    \'custom-fields\',
                                                    \'revisions\') ,
                    \'show_in_nav_menus\' => true ,
                    \'taxonomies\'        => array("pcategory","ptags")
                )
            );

// Register custom taxonomy
register_taxonomy(  "pcategory", 
            array(  "products"  ), 
            array ( "hierarchical"      => true, 
                    "label"             => CUSTOM_MENU_CAT_LABEL, 
                    \'labels\'            => array(   \'name\'              =>  CUSTOM_MENU_CAT_TITLE,
                                                    \'singular_name\'     =>  CUSTOM_MENU_SIGULAR_CAT,
                                                    \'search_items\'      =>  CUSTOM_MENU_CAT_SEARCH,
                                                    \'popular_items\'     =>  CUSTOM_MENU_CAT_SEARCH,
                                                    \'all_items\'         =>  CUSTOM_MENU_CAT_ALL,
                                                    \'parent_item\'       =>  CUSTOM_MENU_CAT_PARENT,
                                                    \'parent_item_colon\' =>  CUSTOM_MENU_CAT_PARENT_COL,
                                                    \'edit_item\'         =>  CUSTOM_MENU_CAT_EDIT,
                                                    \'update_item\'       =>  CUSTOM_MENU_CAT_UPDATE,
                                                    \'add_new_item\'      =>  CUSTOM_MENU_CAT_ADDNEW,
                                                    \'new_item_name\'     =>  CUSTOM_MENU_CAT_NEW_NAME,   ), 
                    \'public\'            => true,
                    \'show_ui\'           => true,
                    "rewrite"           => array(\'slug\' => \'product\',\'with_front\' => true,\'hierarchical\'=>true))
            ); 
我的职能。php包括:

add_filter(\'rewrite_rules_array\', \'mmp_rewrite_rules\');
function mmp_rewrite_rules($rules) {
$newRules  = array();
$newRules[\'product/(.+)/(.+)/(.+)/(.+)/?$\'] = \'index.php?products=$matches[4]\';
$newRules[\'product/(.+)/?$\']                = \'index.php?pcategory=$matches[1]\'; 

return array_merge($newRules, $rules);
}
function filter_post_type_link($link, $post)
{
if ($post->post_type != \'products\')
    return $link;

if ($cats = get_the_terms($post->ID, \'pcategory\'))
{
    $link = str_replace(\'%taxonomy_name%\', get_taxonomy_parents(array_pop($cats)->term_id, \'pcategory\', false, \'/\', true), $link);
}
return $link;
}
add_filter(\'post_type_link\', \'filter_post_type_link\', 10, 2);

// my own function to do what get_category_parents does for other taxonomies
function get_taxonomy_parents($id, $taxonomy, $link = false, $separator = \'/\', $nicename = false, $visited = array()) {    
$chain = \'\';   
$parent = &get_term($id, $taxonomy);

if (is_wp_error($parent)) {
    return $parent;
}

if ($nicename)    
    $name = $parent -> slug;        
else    
    $name = $parent -> name;

if ($parent -> parent && ($parent -> parent != $parent -> term_id) && !in_array($parent -> parent, $visited)) {    
    $visited[] = $parent -> parent;    
    $chain .= get_taxonomy_parents($parent -> parent, $taxonomy, $link, $separator, $nicename, $visited);
}

if ($link) {
    // nothing, can\'t get this working :(
} else    
    $chain .= $name . $separator;
return $chain;    
}
有人知道如何解决这个问题吗?

4 个回复
最合适的回答,由SO网友:jochem 整理而成

在更改某些内容后,我有了使其工作的最终代码。

这是您注册帖子类型的地方。(未更改)

register_post_type( "products", 
            array(  \'label\'             => CUSTOM_MENU_TITLE,
                    \'labels\'            => array(   \'name\'                  =>  CUSTOM_MENU_NAME,
                                                    \'singular_name\'         =>  CUSTOM_MENU_SIGULAR_NAME,
                                                    \'add_new\'               =>  CUSTOM_MENU_ADD_NEW,
                                                    \'add_new_item\'          =>  CUSTOM_MENU_ADD_NEW_ITEM,
                                                    \'edit\'                  =>  CUSTOM_MENU_EDIT,
                                                    \'edit_item\'             =>  CUSTOM_MENU_EDIT_ITEM,
                                                    \'new_item\'              =>  CUSTOM_MENU_NEW,
                                                    \'view_item\'             =>  CUSTOM_MENU_VIEW,
                                                    \'search_items\'          =>  CUSTOM_MENU_SEARCH,
                                                    \'not_found\'             =>  CUSTOM_MENU_NOT_FOUND,
                                                    \'not_found_in_trash\'    =>  CUSTOM_MENU_NOT_FOUND_TRASH ),
                    \'public\'            => true,
                    \'can_export\'        => true,
                    \'show_ui\'           => true, // UI in admin panel
                    \'_builtin\'          => false, // It\'s a custom post type, not built in
                    \'_edit_link\'        => \'post.php?post=%d\',
                    \'capability_type\'   => \'post\',
                    \'menu_icon\'         => get_bloginfo(\'template_url\').\'/images/favicon.ico\',
                    \'hierarchical\'      => true,
                    \'rewrite\'           => array(\'slug\' => \'product/%taxonomy_name%\',\'with_front\' => true,\'hierarchical\'=>true), // Permalinks
                    \'query_var\'         => "products", // This goes to the WP_Query schema
                    \'supports\'          => array(   \'title\',
                                                    \'author\', 
                                                    \'excerpt\',
                                                    \'thumbnail\',
                                                    \'comments\',
                                                    \'editor\', 
                                                    \'trackbacks\',
                                                    \'custom-fields\',
                                                    \'revisions\') ,
                    \'show_in_nav_menus\' => true ,
                    \'taxonomies\'        => array("pcategory","ptags")
                )
            );

// Register custom taxonomy
register_taxonomy(  "pcategory", 
            array(  "products"  ), 
            array ( "hierarchical"      => true, 
                    "label"             => CUSTOM_MENU_CAT_LABEL, 
                    \'labels\'            => array(   \'name\'              =>  CUSTOM_MENU_CAT_TITLE,
                                                    \'singular_name\'     =>  CUSTOM_MENU_SIGULAR_CAT,
                                                    \'search_items\'      =>  CUSTOM_MENU_CAT_SEARCH,
                                                    \'popular_items\'     =>  CUSTOM_MENU_CAT_SEARCH,
                                                    \'all_items\'         =>  CUSTOM_MENU_CAT_ALL,
                                                    \'parent_item\'       =>  CUSTOM_MENU_CAT_PARENT,
                                                    \'parent_item_colon\' =>  CUSTOM_MENU_CAT_PARENT_COL,
                                                    \'edit_item\'         =>  CUSTOM_MENU_CAT_EDIT,
                                                    \'update_item\'       =>  CUSTOM_MENU_CAT_UPDATE,
                                                    \'add_new_item\'      =>  CUSTOM_MENU_CAT_ADDNEW,
                                                    \'new_item_name\'     =>  CUSTOM_MENU_CAT_NEW_NAME,   ), 
                    \'public\'            => true,
                    \'show_ui\'           => true,
                    "rewrite"           => array(\'slug\' => \'product\',\'with_front\' => true,\'hierarchical\'=>true))
            ); 
我的职能。php包括:

add_filter(\'rewrite_rules_array\', \'mmp_rewrite_rules\');
function mmp_rewrite_rules($rules) {
$newRules  = array();
$newRules[\'product/(.+)/(.+)/(.+)/(.+)/?$\'] = \'index.php?products=$matches[4]\';
$newRules[\'product/(.+)/?$\']                = \'index.php?pcategory=$matches[1]\'; 

return array_merge($newRules, $rules);
}
function filter_post_type_link($link, $post)
{
if ($post->post_type != \'products\')
    return $link;

if ($cats = get_the_terms($post->ID, \'pcategory\'))
{
    $link = str_replace(\'%taxonomy_name%\', get_taxonomy_parents(array_pop($cats)->term_id, \'pcategory\', false, \'/\', true), $link);
}
return $link;
}
add_filter(\'post_type_link\', \'filter_post_type_link\', 10, 2);

// my own function to do what get_category_parents does for other taxonomies
function get_taxonomy_parents($id, $taxonomy, $link = false, $nicename = false, $visited = array()) {    
// removed $seperator="/" after $link, otherwise there was a double slash before the post name in the link    
$chain = \'\';   
$parent = &get_term($id, $taxonomy);

if (is_wp_error($parent)) {
    return $parent;
}

if ($nicename)    
    $name = $parent -> slug;        
else    
    $name = $parent -> name;

if ($parent -> parent && ($parent -> parent != $parent -> term_id) && !in_array($parent -> parent, $visited)) {    
    $visited[] = $parent -> parent;    
    $chain .= get_taxonomy_parents($parent -> parent, $taxonomy, $link, $nicename, $visited); //removed $seperator after $link
    $chain .= "/"; //add a / after every category
}

if ($link) {
    // nothing, can\'t get this working :(
} else    
    $chain .= $name; //don\'t need the . $separator anymore
return $chain;    
}

SO网友:s_ha_dum

有一个快速且有点脏的潜在解决方案。我之所以说“潜在”,是因为我无法通过查看代码来发现问题。我只有怀疑。而不是通过那样的分隔符。尝试trailingslashit.

} else    
    $chain .= trailingslashit($name);
return $chain;  
根据你的描述,我在猜测问题出在哪里,但那是唯一$separator 已应用。

有一种情况下,这种简单的修复方法不起作用。如果$name 为空,则字符串中会有一个额外的斜杠,所以最好检查一下,以防万一。

} elseif (!empty($name))     
    $chain .= trailingslashit($name);
return $chain;
试试看。

SO网友:user48107

    $link = str_replace(\'%taxonomy_name%\', get_taxonomy_parents(array_pop($cats)->term_id, \'pcategory\', false, \'/\', true), $link);
需要成为

    $link = str_replace(\'%taxonomy_name%\', get_taxonomy_parents(array_pop($cats)->term_id, \'pcategory\', false, true), $link);
否则,修改后的函数在查找分隔符时出错。

SO网友:T.Todua

也许,该帖子有父帖子ID(检查其父帖子ID)。我怀疑,他们的父帖子不存在,当wordpress试图获取永久链接(包含其父帖子的slugname)时,它只会添加空斜杠。

结束

相关推荐

Update page breaks permalinks

从3.3.1开始,我有一个奇怪的副作用。当更新页面(或创建新页面)时,我的所有永久链接都会中断-单击相关更新页面上的查看页面将导致404找不到。这不会发生在帖子中。我担心我的permalinks/%postname%/新模式可能会出现问题,但我将其替换为YEAR/postname,没有发现任何差异。我检查并发现帖子名称字段中有一些重复,我更改或删除了任何可疑的重复。仍然没有变化。这是我的htaccess-# BEGIN WordPress <IfModule mod_rewrite.c>