在WordPress中为父级和子级自定义类别设置永久链接

时间:2018-09-04 05:14:48

标签: wordpress custom-post-type permalinks custom-taxonomy

如果该帖子重复,对此表示抱歉。我找不到这种情况。 我的主要post_type = post具有默认的永久链接。参见下图。

enter image description here

而且,我有一个自定义帖子类型,我称之为[post_type = fsgallery]。 另外,我创建了一个自定义分类法,名为gallery_category。此类别具有父类别。例如/ movies / drama,/ movies / horror等。

我创建了一些重写规则,但是我不知道如何声明像这样的永久链接http://://xxx.com/workbench-gallery/movies/drama(domain / workbench-gallery / parent_category / child_category)

我安装了许多自定义的Manage permalink插件,但没有找到任何对子类别的父类别有用的插件。 enter image description here

这是我的代码,用于重写自定义永久链接。     

    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); }

0 个答案:

没有答案