CPT和定制分类的固定链接结构问题

时间:2019-07-08 作者:glng

我在一个有许多CPT和自定义分类法的WP网站上工作,但也在该网站的常规博客部分使用常规的“post”帖子类型。

我希望博客帖子和归档以及类别在其永久链接中包含/blog/,但也希望为其他CPT指定永久链接结构,而不需要/blog/干扰。

例如:我有一个“events”CPT,它有一个名为“ecat”的自定义分类法。

现在我有了permalink的存档页sitename.com/events 工作正常

并且单事件页面具有permalink-likesitename.com/events/event-name

但自定义分类法存档位于sitename.com/ecat/ategory-name.我希望自定义分类法在其permalink中包含cpt slug,如:sitename.com/events/ecat/category-name

注意:我不希望单个事件的永久链接中包含分类术语。

我的CPT注册码:

    $labels = array(
    \'name\'                  => _x( \'Events\', \'Post Type General Name\', \'textdomain\' ),
    \'singular_name\'         => _x( \'Event\', \'Post Type Singular Name\', \'textdomain\' ),
    \'menu_name\'             => __( \'Events\', \'textdomain\' ),
    \'name_admin_bar\'        => __( \'Event\', \'textdomain\' ),
    \'archives\'              => __( \'Item Archives\', \'textdomain\' ),
    \'attributes\'            => __( \'Item Attributes\', \'textdomain\' ),
    \'parent_item_colon\'     => __( \'Parent Item:\', \'textdomain\' ),
    \'all_items\'             => __( \'All Items\', \'textdomain\' ),
    \'add_new_item\'          => __( \'Add New Item\', \'textdomain\' ),
    \'add_new\'               => __( \'Add New\', \'textdomain\' ),
    \'new_item\'              => __( \'New Item\', \'textdomain\' ),
    \'edit_item\'             => __( \'Edit Item\', \'textdomain\' ),
    \'update_item\'           => __( \'Update Item\', \'textdomain\' ),
    \'view_item\'             => __( \'View Item\', \'textdomain\' ),
    \'view_items\'            => __( \'View Items\', \'textdomain\' ),
    \'search_items\'          => __( \'Search Item\', \'textdomain\' ),
    \'not_found\'             => __( \'Not found\', \'textdomain\' ),
    \'not_found_in_trash\'    => __( \'Not found in Trash\', \'textdomain\' ),
    \'featured_image\'        => __( \'Featured Image\', \'textdomain\' ),
    \'set_featured_image\'    => __( \'Set featured image\', \'textdomain\' ),
    \'remove_featured_image\' => __( \'Remove featured image\', \'textdomain\' ),
    \'use_featured_image\'    => __( \'Use as featured image\', \'textdomain\' ),
    \'insert_into_item\'      => __( \'Insert into item\', \'textdomain\' ),
    \'uploaded_to_this_item\' => __( \'Uploaded to this item\', \'textdomain\' ),
    \'items_list\'            => __( \'Items list\', \'textdomain\' ),
    \'items_list_navigation\' => __( \'Items list navigation\', \'textdomain\' ),
    \'filter_items_list\'     => __( \'Filter items list\', \'textdomain\' ),
);
$args = array(
    \'label\'                 => __( \'Event\', \'textdomain\' ),
    \'description\'           => __( \'Events custom post type\', \'textdomain\' ),
    \'labels\'                => $labels,
    \'supports\'              => array( \'title\' ),
    \'hierarchical\'          => false,
    \'public\'                => true,
    \'show_ui\'               => true,
    \'show_in_menu\'          => true,
    \'menu_position\'         => 5,
    \'menu_icon\'             => \'dashicons-calendar-alt\',
    \'show_in_admin_bar\'     => true,
    \'show_in_nav_menus\'     => true,
    \'can_export\'            => true,
    \'has_archive\'           => true,
    \'exclude_from_search\'   => false,
    \'publicly_queryable\'    => true,
    \'capability_type\'       => \'page\',
    \'show_in_rest\'          => false,
    \'rewrite\' => array( \'slug\' => \'events\',\'with_front\' => false ),
);
register_post_type( \'events\', $args );
我的自定义分类注册代码:

//Event Categories
$labels = array(
    \'name\'                       => _x( \'Events Categories\', \'textdomain\' ),
    \'singular_name\'              => _x( \'Events Category\', \'textdomain\' ),
    \'menu_name\'                  => __( \'Events Categories\', \'textdomain\' ),
    \'all_items\'                  => __( \'All Items\', \'textdomain\' ),
    \'parent_item\'                => __( \'Parent Item\', \'textdomain\' ),
    \'parent_item_colon\'          => __( \'Parent Item:\', \'textdomain\' ),
    \'new_item_name\'              => __( \'New Item Name\', \'textdomain\' ),
    \'add_new_item\'               => __( \'Add New Item\', \'textdomain\' ),
    \'edit_item\'                  => __( \'Edit Item\', \'textdomain\' ),
    \'update_item\'                => __( \'Update Item\', \'textdomain\' ),
    \'view_item\'                  => __( \'View Item\', \'textdomain\' ),
    \'separate_items_with_commas\' => __( \'Separate items with commas\', \'textdomain\' ),
    \'add_or_remove_items\'        => __( \'Add or remove items\', \'textdomain\' ),
    \'choose_from_most_used\'      => __( \'Choose from the most used\', \'textdomain\' ),
    \'popular_items\'              => __( \'Popular Items\', \'textdomain\' ),
    \'search_items\'               => __( \'Search Items\', \'textdomain\' ),
    \'not_found\'                  => __( \'Not Found\', \'textdomain\' ),
    \'no_terms\'                   => __( \'No items\', \'textdomain\' ),
    \'items_list\'                 => __( \'Items list\', \'textdomain\' ),
    \'items_list_navigation\'      => __( \'Items list navigation\', \'textdomain\' ),
);
$args = array(
    \'labels\'                     => $labels,
    \'hierarchical\'               => true,
    \'public\'                     => true,
    \'show_ui\'                    => true,
    \'show_admin_column\'          => true,
    \'show_in_nav_menus\'          => true,
    \'show_tagcloud\'              => false,
    \'show_in_rest\'               => false,
);
register_taxonomy( \'ecat\', array( \'events\' ), $args );

2 个回复
SO网友:Tom

欢迎使用WP StackExchange!您可能需要尝试以下操作,它使用与寄存器CPT相同的重写设置:

$args = array(
    \'rewrite\'      => array(\'slug\' => \'events/ecat\', \'with_front\' => false)
);
register_taxonomy( \'ecat\', array( \'events\' ), $args );

资料来源:

https://cnpagency.com/blog/the-right-way-to-do-wordpress-custom-taxonomy-rewrites/

https://codex.wordpress.org/Function_Reference/register_taxonomy#Arguments

SO网友:nmr

职位类型前缀post

Here 您将了解如何仅为post类型“post”添加前缀。

同样,可以为类别添加前缀。合适的过滤器挂钩包括:category_rewrite_rules, pre_term_link.

add_filter( \'pre_term_link\', \'se342399_pre_term_link\', 20, 2 );
add_filter( \'category_rewrite_rules\', \'se342399_category_rewrite_rules\', 20 );

function se342399_pre_term_link( $termlink, $term ) 
{
    // add prefix only to links for terms of taxonomy "category"
    if ( $term instanceof \\WP_Term && $term->taxonomy == \'category\' ) {
        $termlink = \'/blog\' . $termlink;
    }
    return $termlink;
}

function se342399_category_rewrite_rules( $category_rewrite ) 
{
    if ( is_array($category_rewrite) ) 
    {
        $new_rules = [];
        foreach( $category_rewrite as $k => $v) {
            $new_rules[ \'blog/\' . $k ] = $v;
        }
        $category_rewrite = $new_rules;
    }
    return $category_rewrite;
}
分类前的CPT段塞ecat在的术语链接中添加自定义帖子类型的slug作为前缀ecat 分类法您可以使用以下方法之一:

添加重写到$args 的参数register_taxonomy() (正如@Tom所建议的)

"rewrite" => array( \'slug\' => \'events/ecat\', \'with_front\' => false, )
或在类别中使用挂钩ecat_rewrite_rules ({$permastructname}_rewrite_rules) 和pre_term_link

相关推荐

Problem with permalinks

我已经更改了类别的基本名称,现在它是“博客”,工作正常。但当我通过/blog/%category%/%postname%/更改结构时。显示404。如果我删除结构中的blog,它会再次工作,但我想使用blog word。问题出在哪里?非常感谢。