自定义固定链接-POST类型-分层分类

时间:2012-03-27 作者:Reitze Bos

我确实折叠了steps / solution from Jeff 它在他的设置上帮了我很多,大约90%的工作正常。但是被贴在了帖子上。它现在加载producten/%taxonomy\\u products%/而没有404错误。但当我进一步点击一篇帖子时,我再次看到404错误。

也许你能看到我做错了什么?

Custom post type and taxonomy settings \'Sorry for the dutch words\' :

    add_action( \'init\', \'products_taxonomy_options\' );
function products_taxonomy_options() {

//=============== Register custom post type - PRODUCTS ================

register_post_type(\'products\',
  array( \'label\'                    => __(\'Producten\'), // Postype
         \'labels\'                   => array( \'name\'                => __(\'Producten\'),
                                              \'singular_name\'       => __(\'Producten\'),
                                              \'add_new\'             => __(\'Product toevoegen\'),
                                              \'add_new_item\'        => __(\'Nieuw product\'),
                                              \'edit_item\'           => __(\'Wijzig product\'),
                                              \'new_item\'            => __(\'Nieuw product\'),
                                              \'all_items\'           => __(\'Alle producten\'),
                                              \'view_item\'           => __(\'Bekijk producten\'),
                                              \'search_items\'        => __(\'Zoek producten\'),
                                              \'not_found\'           => __(\'Er zijn geen producten gevonden\'),
                                              \'not_found_in_trash\'  => __(\'Geen producten gevonden in de prullenbak\')                                   
                                    ),
          \'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" => "producten"), // Permalinks
          \'supports\'                => array(   \'title\',
                                                \'editor\',                                                                                               
                                                \'thumbnail\',
                                    ),               
          \'show_in_menu\'            => true,
          \'taxonomies\'              => array(\'productcat\')
        )
    );


//=============== Register custom taxonomy - PRODUCTS CATEGORY ================ 

register_taxonomy(  "productcat", 
                array(  "products"  ), 
                array ( "hierarchical"      => true, 
                        "label"             => "Product categorieën", 
                        \'labels\'            => array(   \'name\'              => __(\'Product categorieën\'),
                                                        \'singular_name\'     => __(\'Product categorie\'),
                                                        \'search_items\'      => __(\'Categorieën zoeken\'),
                                                        \'popular_items\'     => __(\'Populaire categorieën\'),
                                                        \'all_items\'         => __(\'Alle categorieën\'),
                                                        \'parent_item\'       => __(\'Huidige categorie\'),
                                                        \'parent_item_colon\' => __(\'Huidige categorie:\'),
                                                        \'edit_item\'         => __(\'Wijzig categorie\'),
                                                        \'update_item\'       => __(\'Update categorie\'),
                                                        \'add_new_item\'      => __(\'Nieuwe categorie toevoegen\'),
                                                        \'new_item_name\'     => __(\'Nieuwe categorie\')
                                            ), 
                          \'show_ui\'         => false,
                          \'query_var\'       => true,
                          \'_builtin\'        => false,
                          \'paged\'           => true,
                          \'rewrite\'         => array(   \'hierarchical\'      => true, 
                                                        \'slug\'              =>\'products\',
                                                        \'with_front\'        => false
                                            ),
                        )
                );

}

Jeff\'s settings:

add_filter(\'rewrite_rules_array\', \'mmp_rewrite_rules\');
function mmp_rewrite_rules($rules) {
    $newRules  = array();
    $newRules[\'producten/(.+)/(.+)/(.+)/(.+)/?$\'] = \'index.php?products=$matches[4]\'; // my custom structure will also have the post name as the 5th uri segment
    $newRules[\'producten/(.+)/?$\']                = \'index.php?productcat=$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, \'productcat\'))
    {
        $link = str_replace(\'%taxonomy_products%\', get_taxonomy_parents(array_pop($cats)->term_id, \'productcat\', false, \'/\', true), $link); // see custom function defined below
    }
    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;    
}

Permalink settings:

/producten/%taxonomy_products%/%postname%/

Working with the Taxonomic SEO Permalink plugin

谢谢

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

感谢您参考我的解决方案。但是,您忘记了一个部分-定义cpt的重写。从我的解决方案:

首先,在定义自定义帖子类型和分类法时,要正确使用slug:对于自定义帖子类型,应该是basename/%taxonomy_name% 分类法中的slug应该是basename。别忘了在taxonomy rewrite数组中添加“Hierarchy”=>true,以在url中获取嵌套术语。另外,请确保在这两种情况下query\\u var都设置为true。

因此,在您的情况下,您对自定义帖子类型的重写应该是producten/%productcat%. 而且在我的filter_post_type_link 需要更改的功能%taxonomy_products%%productcat%.

希望这一切都能成功!

结束

相关推荐

Update all posts at once

我已经在我的帖子中添加了一个自定义分类法,它将在post\\u update操作中获得iteself的值。一次更新所有帖子的SQL查询是什么,以便为每个帖子添加分类法值。从SQL更新还会调用wordpress的update\\u post操作吗?