Taxonomy redirect issue

by jhon jhon doe   Last Updated May 28, 2018 10:08 AM

After create custom post want to remove taxonomy slug from post url website name seersco.com/article but it show double article/article one for website and other taxonomy name. so want to remove taxonomy name. Heres' my code.

function article_init()
{
    $args = array(
      'label' => 'Seerco Articles',
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => true,
      'rewrite' => array( 'slug' => '/%show_category%', 'with_front' => true ),
      'taxonomies' => array( 'show_category' ),
        'query_var' => true,
    //  'has_archive' => 'articles',
        'menu_icon' => 'dashicons-book',

        'supports' => array( 
            'title',
            'editor',
            'excerpt',
            'trackbacks',
            'custom-fields',
            'comments',
            'revisions',
            'thumbnail',
            'author',
            'page-attributes',)
        );
    register_post_type( 'article', $args );
    ///////////////// Category /////////////////////////////////////////////////////////////
   register_taxonomy( 'articles', array('article'), array(
        'hierarchical' => true, 
        'label' => 'categories', 
        'singular_label' => 'categories', 
        'rewrite' => array( 'slug' => 'articles', 'hierarchical' => true, 'with_front'=> true ),
        'supports' => array( 'thumbnail' ),
        )
    );
         flush_rewrite_rules();
        ///////////////// Category /////////////////////////////////////////////////////////////


    register_taxonomy_for_object_type( 'articles', 'article' ); // Better be safe than sorry   



}
add_action( 'init', 'article_init' );


function wpa_show_permalinks( $post_link, $post ){

    if ( is_object( $post ) && $post->post_type == 'article' ){


        $terms = wp_get_object_terms( $post->ID, 'articles' );
        //var_dump( $terms);
        if( $terms ){
            return str_replace( '%show_category%' , $terms[0]->slug , $post_link );
        }
    }
    return $post_link;
}
add_filter( 'post_type_link', 'wpa_show_permalinks', 1, 2 );

function so23698827_add_rewrite_rules( $rules ) {
  $new = array();
  $new['articles/([^/]+)/(.+)/?$'] = 'index.php?article=$matches[2]';
  $new['%show_category%/(.+)/?$'] = 'index.php?%show_category%=$matches[1]';

  return array_merge( $new, $rules ); // Ensure our rules come first
}
add_filter( 'rewrite_rules_array', 'so23698827_add_rewrite_rules' );


Related Questions


WordPress 3.4 Permalinks Redirection Problem

Updated April 23, 2015 20:03 PM

Permanent link doesn't work with utf-8 characters

Updated August 02, 2016 08:03 AM

Get canonical link

Updated April 05, 2018 12:08 PM