如果这篇文章重复,抱歉 . 我找不到这样的情况 . 我的主post_type = post有默认的永久链接 . 见下图 .
而且,我有一个自定义的帖子类型,我称之为[post_type = fsgallery] . 此外,我创建了一个自定义分类法并称为gallery_category . 此类别包含父类别 . 例如/电影/戏剧,/电影/恐怖等 .
我创建了一些重写规则,但我不知道如何声明这样的永久链接http :: //xxx.com/workbench-gallery/movies/drama(domain / workbench-gallery / parent_category / child_category)
我安装了许多自定义管理永久链接插件,但我没有找到任何对子类别的父类别有用的插件 .
这是我重写自定义永久链接的代码 .
function wpa_show_permalinks($post_link, $post)
{
if (is_object($post) && $post->post_type == 'fsgallery') {
$terms = wp_get_object_terms($post->ID, 'fsgallery_category');
if ($terms) {
$primary_cat_id = get_post_meta($post->ID, '_yoast_wpseo_primary_fsgallery_category', true);
foreach ($terms as $term_key => $term_object) {
if ($term_object->term_id == $primary_cat_id) {
// Return this as the primary term
return str_replace('%fsgallery_category%', $terms[$term_key]->slug, $post_link);
}
}
} else {
return str_replace('%fsgallery_category%', '', $post_link);
}
}
return $post_link;
}
add_filter('post_type_link', 'wpa_show_permalinks', 1, 2);
</pre>
I found my parent and child categories, but how to set both of them to the URL?.
UPDATE *********************************************************************
Here is my register my post type codes:
public static function register_gallery_post_type()
{
$labels = array(
'name' => _x('Galleries', 'post type general name', 'customPluginName'),
'singular_name' => _x('Gallery', 'post type singular name', 'customPluginName'),
'menu_name' => _x('FormaGallery', 'admin menu', 'customPluginName'),
'name_admin_bar' => _x('Gallery', 'add new on admin bar', 'customPluginName'),
'add_new' => _x('Add New', 'gallery', 'customPluginName'),
'add_new_item' => __('Add New Gallery', 'customPluginName'),
'new_item' => __('New Gallery', 'customPluginName'),
'edit_item' => __('Edit Gallery', 'customPluginName'),
'view_item' => __('View Gallery', 'customPluginName'),
'all_items' => __('All Galleries', 'customPluginName'),
'search_items' => __('Search Galleries', 'customPluginName'),
'parent_item_colon' => __('Parent Galleries:', 'customPluginName'),
'not_found' => __('No galleries found.', 'customPluginName'),
'not_found_in_trash' => __('No galleries found in Trash.', 'customPluginName')
);
$supports = array(
'title',
'editor',
'thumbnail',
'comments',
'custom-fields',
'excerpt',
'author',
'revisions',
'post-formats'
);
$args = array(
'labels' => $labels,
'description' => __('Description.', 'customPluginName'),
'supports' => $supports,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'taxonomies' => array('fsgallery_category', 'post_tag'),
'menu_icon' => 'dashicons-format-gallery',
'rewrite' => array('slug' => 'workbench-gallery/%fsgallery_category%', 'with_front' => false),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'_builtin' => false, // It's a custom post type, not built in!,
'menu_position' => null
);
register_post_type('fsgallery', $args);
}
Here is my register taxonomy code:
public static function register_fsgallery_taxonomy()
{
$labels = array(
'name' => _x('Gallery categories', 'taxonomy general name', 'customPluginName'),
'singular_name' => _x('Gategory for gallery', 'taxonomy singular name', 'customPluginName'),
'search_items' => __('Search in Gallery categories', 'customPluginName'),
'all_items' => __('All Gallery categories', 'customPluginName'),
'parent_item' => __('Parent Category', 'customPluginName'),
'parent_item_colon' => __('Parent Category:', 'customPluginName'),
'edit_item' => __('Edit Category', 'customPluginName'),
'update_item' => __('Update Category', 'customPluginName'),
'add_new_item' => __('Add New Category', 'customPluginName'),
'new_item_name' => __('New Category Category', 'customPluginName'),
'menu_name' => __('Category', 'customPluginName'),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'slug' => 'fsgallery',
'query_var' => true,
'rewrite' => array('slug' => 'gallery', 'with_front' => true),
);
register_taxonomy('fsgallery_category', array('fsgallery'), $args);
}