【问题标题】:Wordpress Custom rewrite not workingWordpress 自定义重写不起作用
【发布时间】:2014-03-26 13:39:12
【问题描述】:

我尝试了很多不同的东西,阅读了 30 篇不同的帖子,但我无法让自定义重写与自定义 post_type 和自定义分类一起使用。

这是我的代码:

    function product_register() {

    $labels = array(
        'name' => __('My Products', 'post type general name'),
        'singular_name' => __('Product', 'post type singular name'),
        'add_new' => __('Add New', 'Product'),
        'add_new_item' => __('Add New Product'),
        'edit_item' => __('Edit Product'),
        'new_item' => __('New Product'),
        'view_item' => __('View This Product'),
        'search_items' => __('Search Products'),
        '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,
        'menu_icon' => plugin_dir_url( __FILE__ ).'/icon.png',
        'rewrite' => array( 'slug' => 'products/%taxonomy_name%', 'with_front'      => false,'hierarchical'     => true),
        'capability_type' => 'post',
        'hierarchical' >= false,
        'menu_position' => null,
        'supports' => array('title','editor','thumbnail','revisions')
      ); 

    register_post_type( 'products' , $args );
}



function create_product_taxonomies() {

    $labels = array(
        'name'              => _x( 'Categories', 'taxonomy general name' ),
        'singular_name'     => _x( 'Category', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Categories' ),
        'all_items'         => __( 'All Categories' ),
        'parent_item'       => __( 'Parent Category' ),
        'parent_item_colon' => __( 'Parent Category:' ),
        'edit_item'         => __( 'Edit Category' ),
        'update_item'       => __( 'Update Category' ),
        'add_new_item'      => __( 'Add New Category' ),
        'new_item_name'     => __( 'New Category Name' ),
        'menu_name'         => __( 'Categories' ),
    );

    $rewrite = array(
        'slug'              => 'products',
        'with_front'        => false,
        'hierarchical'      => true
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => $rewrite, // this makes hierarchical URLs
    );

    register_taxonomy( 'product_category', array( 'products' ), $args );


    $labels = array(
        'name'              => _x( 'Sales', 'taxonomy general name' ),
        'singular_name'     => _x( 'Sale', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Sales' ),
        'all_items'         => __( 'All Sales' ),
        'parent_item'       => __( 'Parent Sale' ),
        'parent_item_colon' => __( 'Parent Sale:' ),
        'edit_item'         => __( 'Edit Sale' ),
        'update_item'       => __( 'Update Sale' ),
        'add_new_item'      => __( 'Add New Sale' ),
        'new_item_name'     => __( 'New Sale Name' ),
        'menu_name'         => __( 'Sale' ),
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'product_sale', 'hierarchical' =>true ),
    );

    register_taxonomy( 'product_sale', array( 'products' ), $args );    


}

function add_rules(){

    add_rewrite_rule( '^products/(.+?)/(.+?)/$', 'products.php?posttype=$matches[2]', 'top' );
    add_rewrite_rule( '^products/(.+?)/(.+?)/(.+?)$', 'products.php?taxonomy=$matches[3]', 'top' );
    add_rewrite_rule( '^products/(.+?)/(.+?)/?$', 'products.php?taxonomy=$matches[2]', 'top' ); 
add_rewrite_rule( '^products/(.+?)/(.+?)/(.+?)/(.+?)$', 'products.php?taxonomy=$matches[3]&products=$matches[4]', 'top' );  
}

    /* Register products taxonomies */
add_action('init', 'create_product_taxonomies', 0 );

/* Register custom post types on the 'init' hook. */
add_action('init', 'product_register');

function filter_post_type_link($link, $post)
{

    if ($post->post_type != 'products')
        return $link;
    if ($cats = get_the_terms($post->ID, 'product_category'))
    {
        $replace = get_taxonomy_parents(array_pop($cats)->term_id, 'product_category', false, '/', true);
        $link = str_replace('%taxonomy_name%', rtrim($replace, '/'), $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;    
}

所以,当我访问 example.com/cat_1/cat_2/cat_3 时,一切正常。它显示了正确的类别。当我最后添加实际产品时,它给了我一个错误 404。我还应该注意,即使重写是针对 products.php,我实际上必须使用 taxonomy.php 才能显示它。

我是否在某处遗漏了一些配置细节?

【问题讨论】:

    标签: wordpress taxonomy hierarchical


    【解决方案1】:

    呃!回答了我的问题,现在我真的很难用头撞墙.... 更改:

    add_rewrite_rule( '^products/(.+?)/(.+?)/(.+?)/(.+?)$', 'products.php?taxonomy=$matches[3]&products=$matches[4]', 'top' );  
    

    收件人:

    add_rewrite_rule( '^products/(.+?)/(.+?)/(.+?)/(.+?)$', 'index.php?taxonomy=$matches[3]&products=$matches[4]', 'top' ); 
    

    需要使用 index.php 来显示帖子的详细信息。人活着……

    【讨论】:

      猜你喜欢
      • 2015-01-31
      • 1970-01-01
      • 2015-11-08
      • 2017-09-08
      • 1970-01-01
      • 2011-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多