Monday, January 2, 2023
HomeWordPress DevelopmentCustomized Taxonomy rewrite rule working for every thing apart from singles

Customized Taxonomy rewrite rule working for every thing apart from singles


I’ve been writing a {custom} rewrite for a CPT, its all working apart from the cpt singles.
Its actually odd because the permalink is appropriate if you edit a publish. Nonetheless if you navigate to hyperlink it isn’t?

Submit Kind declaration

    perform bike_post_init()
{   
    $menu_icon_url = get_theme_file_path('belongings/dist/img/misc/bike.svg');
    $bike_icon = file_get_contents($menu_icon_url);
    $singular="Bike";
    $plural="Bikes";
    $labels = array(
        'identify'               => __($plural),
        'singular_name'      => __($singular),
        'add_new'            => __('Add New ' . $singular),
        'add_new_item'       => __('Create New ' . $singular),
        'edit'               => __('Edit ' . $singular),
        'edit_item'          => __('Edit ' . $singular),
        'new_item'           => __('New ' . $singular),
        'view'               => __('View ' . $plural),
        'view_item'          => __('View ' . $singular),
        'search_items'       => __('Search ' . $plural),
        'not_found'          => __('No ' . $plural .' discovered'),
        'not_found_in_trash' => __('No ' . $plural . ' present in trash'),
        'mother or father'             => __('Mum or dad ' . $singular),
        'featured_image'        => _x($singular . ' Cowl Picture', 'Overrides the “Featured Picture” phrase for this publish kind.', $singular),
        'set_featured_image'    => __('Set ' . $singular .' Cowl Picture', $singular),
    );

    $args = array(
        'labels'                => $labels,
        'description'           => __('That is the place you'll be able to create new '. $plural .' in your website.'),
        'public'                => true,
        'publicly_queryable'    => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'query_var'             => true,
        'rewrite'               => array('slug' => 'bikes/%bike_cat%', 'hierarchical' => true, 'with_front' => false),
        'capability_type'       => 'publish',
        'has_archive'           => false,
        'exclude_from_search'   => false,
        // 'menu_position'         => 2,
        'menu_icon'             => 'knowledge:picture/svg+xml;base64,' . base64_encode( $bike_icon ),
        'hierarchical'          => true,
        '_builtin'              => false,
        'show_in_rest'          => true,
        'helps'              => array('title', 'thumbnail', 'custom-fields', 'page-attributes'),
    );
    // REGISTER POST TYPE
    register_post_type($singular, $args);
}

Taxonomy Declaration

perform create_bike_taxonomies()
{
    // Add new taxonomy, make it hierarchical (like classes)
    $labels = array(
        'identify'              => _x('Classes', 'taxonomy normal identify'),
        'singular_name'     => _x('Class', 'taxonomy singular identify'),
        'search_items'      =>  __('Search Classes'),
        'all_items'         => __('All Classes'),
        'parent_item'       => __('Mum or dad Classes'),
        'parent_item_colon' => __('Mum or dad Classes:'),
        'edit_item'         => __('Edit Class'),
        'update_item'       => __('Replace Class'),
        'add_new_item'      => __('Add New Class'),
        'new_item_name'     => __('New Class Identify'),
        'menu_name'         => __('Class'),
    );

    register_taxonomy('bike_cat', array('bike'), array(
        'hierarchical'  => true,
        'labels'        => $labels,
        'show_ui'       => true,
        'show_in_rest' => true,
        'query_var'     => true,
        //'rewrite'     => true,
        'rewrite'       => array('slug' => 'bike','hierarchical' => true),
        'default_term'      => [
            'name' => 'Uncategorised', //(string) Name of default term.
            'slug' => 'uncategorised', //(string) Slug for default term.
            'description' => '', //(string) Description for default term.
        ],
    ));

}
add_action('init', 'create_bike_taxonomies', 0);
register_taxonomy_for_object_type('bike_cat', 'bike');
add_action('admin_init', 'flush_rewrite_rules');

Permalink Rewrite perform

add_filter('post_type_link', 'bike_post_type_link', 10, 2);
perform bike_post_type_link($hyperlink, $publish)
{
    if ($post->post_type === 'bike') {
        if ($phrases = get_the_terms($post->ID, 'bike_cat')){
            $slug = [];
            foreach ($phrases as $time period) {
                //@hyperlink https://developer.wordpress.org/reference/features/sanitize_title_with_dashes/
                if ($term->mother or father == 0) {
                    array_unshift($slug, sanitize_title_with_dashes($term->identify));
                } else {
                    array_push($slug, sanitize_title_with_dashes($term->identify));
                };
            };
            $hyperlink = str_replace('%bike_cat%',be a part of("https://wordpress.stackexchange.com/", $slug), $hyperlink);
        }else{
            $hyperlink = str_replace('%bike_cat%', 'uncategorised', $hyperlink);
        }
    }

    return $hyperlink;
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments