【问题标题】:Wordpress URL Rewrite to sync custom taxonomy and post typeWordpress URL 重写以同步自定义分类和帖子类型
【发布时间】:2019-09-17 07:07:56
【问题描述】:

这是我的自定义帖子类型和分类的当前 URL。我注意到自定义帖子类型和分类法的重写 slug 不能既是“库存”又是中断。所以我选择了“库存类别”和“库存”。

当前网址:

类别 http://localhost:3000/inventory-category/actual-category-name/

单人 http://localhost:3000/inventory/product-title

我想要:

类别 http://localhost:3000/inventory/actual-category-name/

单人 http://localhost:3000/inventory/actual-category-name/product-title

所以我有 2 个问题

  1. 如何让帖子类型和分类法共享相同的 URL“inventory”而不是使用“inventory-category”。

  2. 如何让单曲使用实际的类别名称?所以它可能是:

http://localhost:3000/inventory/cars/product-title

http://localhost:3000/inventory/trucks/product-title

// register a new list of categories for custom post type
function so_inventory_categories() {
    $args = array(
        'hierarchical'       => true,
        'show_ui'            => true,
        'show_admin_column'  => true,
        'query_var'          => true,
        'public'             => true,
        'publicly_queryable' => true,
        'rewrite'            => array('with_front' => false, 'slug' => 'inventory-category'),
    );

    register_taxonomy( 'inventory_categories', array( 'inventory' ), $args );
}
add_action( 'init', 'so_inventory_categories' );

function so_posttype_inventory() {
    $args = array(
        'labels'              => ['name'=>'Inventory'],
        'supports'            => array( 'title', 'editor' ),
        'taxonomies'          => array( 'inventory_categories' ),
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => false,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
        'rewrite'             => array('with_front' => false,'slug' => 'inventory'),
        'menu_icon'           => 'dashicons-plus-alt',
    );
    register_post_type( 'inventory', $args );
}

add_action( 'init', 'so_posttype_inventory', 0 );

【问题讨论】:

    标签: php wordpress url-rewriting url-routing


    【解决方案1】:

    您应该使用重写规则来完成这项工作。像这样的:

    add_action('init', 'custom_rewrite_basic');
    function custom_rewrite_basic() {
        add_rewrite_rule('^inventory/([^/]+)/?$', 'index.php?taxonomy=inventory_category&inventory_category=$matches[1]', 'top');
        add_rewrite_rule('^inventory/([^/]+)/([^/]+)/?$', 'index.php?post_type=inventory&inventory=$matches[2]', 'top');
    }
    

    我尚未对此进行测试,因此可能仍需要一些调整。

    【讨论】:

    • 这行得通!谢谢!下一个问题。我现在有多个指向同一页面的网址。如何删除旧网址?除了做 301 重定向...
    • 请注意,覆盖自定义帖子类型和分类的默认 URL 存在一些问题。例如,当您在管理面板中单击视图类别或帖子类型时,wordpress 只知道默认 URL。所以如果你重新编写,我们还需要弄清楚如何告诉 Wordpress 新 URL 是什么。并通过 Yoast 和网站的站点地图告诉 SEO。
    • 您应该为要更改的永久链接添加一个过滤器,如下所示: function custom_inventory_link( $url, $post ) { if ( 'inventory' === get_post_type( $post ) ) { $term = wp_get_post_terms($post->ID, 'inventory_category')[0];返回 get_term_link( $term ) 。 $post->名称。 '/'; } 返回 $url; } add_filter('post_type_link', 'custom_inventory_link', 10, 2);
    • 对于类别: function custom_inventory_category_link( $url, $term, $taxonomy ) { if ( 'inventory_category' === $taxonomy ) { return str_replace( 'inventory-category', 'inventory' , $url ); } 返回 $url; } add_filter('term_link', 'custom_inventory_category_link', 10, 2);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    • 2013-08-03
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多