0

I have a custom post type 'University' on my website. Each university has post type 'Students'. So this is how it appears on hyperlinks:

www.abc.com/university/ali

now I want to make another university list and want the information to be displayed as

  • www.abc.com/2015/university/ali
  • www.abc.com/2016/university/ali
  • www.abc.com/2017/university/ali

How can I do that?

1

I have created 'project' posttype and 'project_category' texonomy.Now you have to do is that go to add new project and add that project category that you want to show before your custom posttype name and permalink means add any post inside 2016 category.And set link as custom posttype '/%postname%/.try this code in your functions.php file and check .And do same with you custom posttype I am getting this 'www.abc.com/2016/projects/test-permalink/'.

   add_filter( 'pre_get_posts', 'query_post_type' );
    function query_post_type( $query ) {
      if ( is_category() ) {
    $post_type = get_query_var( 'post_type' );
    if ( $post_type ) {
        $post_type = $post_type;
    } else { $post_type = array( 'nav_menu_item', 'post', 'projects'         ); // End if().
    }
    $query->set( 'post_type',$post_type );
    return $query;
     }
  }

   function custom_post_type() {

// Set UI labels for Custom Post Type
$labels = array(
    'name'                => _x( 'projects', 'Post Type General Name', 'namo' ),
    'singular_name'       => _x( 'projects', 'Post Type Singular Name', 'namo' ),
    'menu_name'           => __( 'projects', 'textdomain' ),
    'parent_item_colon'   => __( 'Parent projects', 'textdomain' ),
    'all_items'           => __( 'All projects', 'textdomain' ),
    'view_item'           => __( 'View projects', 'textdomain' ),
    'add_new_item'        => __( 'Add New projects', 'textdomain' ),
    'add_new'             => __( 'Add New', 'textdomain' ),
    'edit_item'           => __( 'Edit projects', 'textdomain' ),
    'update_item'         => __( 'Update projects', 'textdomain' ),
    'search_items'        => __( 'Search projects', 'textdomain' ),
    'not_found'           => __( 'Not Found', 'textdomain' ),
    'not_found_in_trash'  => __( 'Not found in Trash', 'textdomain' ),
);

// Set other options for Custom Post Type

$args = array(
    'label'               => __( 'projects', 'textdomain' ),
    'description'         => __( 'projects news and reviews', 'textdomain' ),
    'labels'              => $labels,
    'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields' ),
    'hierarchical'        => false,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 5,
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'capability_type'     => 'page',
    'rewrite' => array(
        'slug' => '%project_category%/projects',
        'with_front' => true,
      ),

    // This is where we add taxonomies to our CPT
    //'taxonomies'          => array( 'category' ),
);

// Registering your Custom Post Type
register_post_type( 'projects', $args );
// Add new taxonomy, NOT hierarchical (like tags)
$labels = array(
    'name'                       => _x( 'project_category', 'taxonomy general name', 'textdomain' ),
    'singular_name'              => _x( 'project_category', 'taxonomy singular name', 'textdomain' ),
    'search_items'               => __( 'Search project_categorys', 'textdomain' ),
    'popular_items'              => __( 'Popular project_categorys', 'textdomain' ),
    'all_items'                  => __( 'All project_categorys', 'textdomain' ),
    'parent_item'                => null,
    'parent_item_colon'          => null,
    'edit_item'                  => __( 'Edit project_category', 'textdomain' ),
    'update_item'                => __( 'Update project_category', 'textdomain' ),
    'add_new_item'               => __( 'Add New project_category', 'textdomain' ),
    'new_item_name'              => __( 'New project_category Name', 'textdomain' ),
    'separate_items_with_commas' => __( 'Separate project_category with commas', 'namo' ),
    'add_or_remove_items'        => __( 'Add or remove project_category', 'namo' ),
    'choose_from_most_used'      => __( 'Choose from the most used project_category', 'namo' ),
    'not_found'                  => __( 'No project_category found.', 'namo' ),
    'menu_name'                  => __( 'project_category', 'namo' ),
);

$args1 = array(
    'hierarchical'          => false,
    'labels'                => $labels,
    'show_ui'               => true,
    'show_admin_column'     => true,
    'update_count_callback' => '_update_post_term_count',
    'query_var'             => true,
    //'rewrite'               => array( 'slug' => 'project_category' ),
     'rewrite' => array(
        'slug' => 'project_category',
        'with_front' => true,
        ),
        );
        register_taxonomy( 'project_category', 'projects', $args1 );

   }



   add_action( 'init', 'custom_post_type', 0 );


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

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


  function so23698827_filter_post_type_link( $link, $post ) {
    if ( $post->post_type == 'projects' ) {
      if ( $cats = get_the_terms( $post->ID, 'project_category' ) ) {
        $link = str_replace( '%project_category%', current( $cats )->slug, $link );
     }
  }
  return $link;
     }  
    add_filter( 'post_type_link', 'so23698827_filter_post_type_link', 10, 2 );
|improve this answer|||||
0

Use 'custom-post-type-permalinks' plugin

and insert '/%year%/%postname%/' to your custom post type text field it will show link like:

    www.abc.com/university/2015/ali
    www.abc.com/university/2016/ali
|improve this answer|||||
  • This is not how I want to show. I want to keep year at root. www.abc.com/2015/university/ali – Rookie Dec 1 '16 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.