how to create single page for custome post type, custome taxonomy with rewrite rule

Question

i am creating wordpress website. i have made a cpt like ‘news_post_type’ with rewrite url. but my single page and archive page not working. here is my both files name (archive-news.php, single-news_post_type.php)

below is custome post type url structure

abc.com/news-updates/test/598-test

i have used below code for custome post type

for register post type

add_action('init', 'cpt_news');

function cpt_news() {

$supports = array(
'title', // post title
'editor', // post content
'author', // post author
'thumbnail', // featured images
'excerpt', // post excerpt
'custom-fields', // custom fields
'comments', // post comments
'revisions', // post revisions
'post-formats', // post formats
);

$labels = array(
'name' => _x('News', 'plural', 'snt'),
'singular_name' => _x('News', 'singular', 'snt'),
'menu_name' => _x('News', 'admin menu', 'snt'),
'name_admin_bar' => _x('News', 'admin bar'),
'add_new' => _x('Add New', 'add new'),
'add_new_item' => __('Add New News'),
'new_item' => __('New News'),
'edit_item' => __('Edit News'),
'view_item' => __('View News'),
'all_items' => __('All News'),
'search_items' => __('Search News'),
'not_found' => __('No News found.'),
);

$args = array(
'supports' => $supports,
'labels' => $labels,
'description' => 'Holds our News and specific data',
'public' => true,
'taxonomies' => array( 'news_year' ),
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'can_export' => true,
'capability_type' => 'post',
'show_in_rest' => true,
'query_var' => true,
'rewrite' => true,
'has_archive' => true,
'hierarchical' => true,
'menu_position' => 6,
'menu_icon' => 'dashicons-megaphone',
);

register_post_type('news_post_type', $args); // Register Post type
}

For Custom texonomy

function news_year() {

    $labels = array(
        'name'                       => _x( 'Year', 'Taxonomy General Name', 'snt' ),
        'singular_name'              => _x( 'Year', 'Taxonomy Singular Name', 'snt' ),
        'menu_name'                  => __( 'Years', 'snt' ),
        'all_items'                  => __( 'All Items', 'snt' ),
        'parent_item'                => __( 'Parent Year', 'snt' ),
        'parent_item_colon'          => __( 'Parent Year:', 'snt' ),
        'new_item_name'              => __( 'New Year Name', 'snt' ),
        'add_new_item'               => __( 'Add New Year', 'snt' ),
        'edit_item'                  => __( 'Edit Year', 'snt' ),
        'update_item'                => __( 'Update Year', 'snt' ),
        'view_item'                  => __( 'View Year', 'snt' ),
        'separate_items_with_commas' => __( 'Separate Year with commas', 'snt' ),
        'add_or_remove_items'        => __( 'Add or Remove Year', 'snt' ),
        'choose_from_most_used'      => __( 'Choose from the most used', 'snt' ),
        'popular_items'              => __( 'Popular Year', 'snt' ),
        'search_items'               => __( 'Search Year', 'snt' ),
        'not_found'                  => __( 'Not Found', 'snt' ),
        'no_terms'                   => __( 'No items', 'snt' ),
        'items_list'                 => __( 'Year list', 'snt' ),
        'items_list_navigation'      => __( 'Year list navigation', 'snt' ),
    );

    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'query_var'                  => true,
        'show_in_rest'               => true, // for show in cpt
        'rewrite'                    => array('slug' => 'news-year')
    );
    register_taxonomy( 'news_year', array( 'news_post_type' ), $args );

}
add_action( 'init', 'news_year', 0 );

Post type link hook

function change_link( $permalink, $post ) {
    // die();
    
    if( $post->post_type == 'news_post_type' ) {
        $resource_terms = get_the_terms( $post, 'news_year' );
        $term_slug = '';
        if( ! empty( $resource_terms ) ) {
            foreach ( $resource_terms as $k=>$term ) {

                // The featured resource will have another category which is the main one
                if( $k == 0 ) {
                     $term_slug = $term->slug;;
                } 
                 
            }
        }
        $permalink = get_home_url() ."/news-updates/" . $term_slug . '/' . $post->ID.'-'.$post->post_name;
    }
    return $permalink;

}
add_filter('post_type_link','change_link',10,2);
0
Sandeep 2 months 0 Answers 8 views 0

Leave an answer