自定义WordPress永久链接结构未针对所有自定义帖子类型更改

时间:2018-05-10 20:19:20

标签: wordpress custom-post-type permalinks

我将WordPress网站中的默认永久链接结构更改为:www.example.com/blog/%postname%/。我想要' / blog /'部分仅适用于帖子。我有自定义的帖子类型,如' education','案例研究'和'资源'在哪里我不想要博客'成为网址的一部分。我在register_post_type函数中添加了一个重写参数,如下所示:'rewrite' => array('slug' => $slug , 'with_front' => false)。它适用于所有自定义帖子类型,除了资源'。我添加了代码来刷新重写规则,我重置永久链接没有任何成功。有什么我想念的吗?

解决方案(更新): 这是我注册自定义帖子类型的代码:

/** Remove extraneous resource custom post type */
add_action('init','delete_post_type', 5);
function delete_post_type(){
  unregister_post_type( 'resource' );
}

/** Register custom post types */
add_action( 'init', 'add_custom_register_post_types', 99 );
function add_custom_register_post_types() {    
  custom_register_post_type( 'Resource', 'Resources', 'resource', 'resources', true, array( 'title', 'editor', 'thumbnail' ), true, array( 'category' ) );
  custom_register_post_type( 'Case Study', 'Case Studies', 'case_study', 'case-studies', true, array( 'title', 'editor', 'thumbnail', ), true, array( 'category', 'post_tag' ) );
  custom_register_post_type( 'Product', 'Products', 'product', 'product', true, array( 'title', 'thumbnail' ), true, array(), false );
  custom_register_post_type( 'Guest Speaker', 'Guest Speakers', 'guest-speaker', 'guest-speaker', false, array( 'title', 'editor', 'thumbnail' ), true, array(), false );
  custom_register_post_type( 'News and Media Post', 'News and Media', 'news-and-media', 'company/news-and-media', true, array( 'title', 'editor' ), true, array() );
  custom_register_post_type( 'Education', 'Education', 'education', 'education1', true, array( 'title', 'editor', 'thumbnail', 'custom-fields' ), true, array( 'category', 'post_tag' ) );

  //flush_rewrite_rules();
}

function custom_register_post_type( $type, $types, $cpt_name, $slug, $search=false, $supports, $showui=true, $taxonomies, $has_archive = true ){

  $type2=strtolower( $type );
  $types2=strtolower( $types );

  $labels = array(
    'name' => $type,
    'singular_name' => $type,
    'add_new' => 'Add New',
    'add_new_item' => 'Add New ' .$type,
    'edit_item' => 'Edit '.$type,
    'new_item' => 'New '.$type,
    'view_item' => 'View '.$type,
    'search_items' => 'Search '.$types,
    'not_found' =>  'No '.$types2.' found',
    'not_found_in_trash' => 'No '.$types2.' found in Trash',
    'parent_item_colon' => '',
    'menu_name' => $types
  );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => $showui,
        'query_var' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'has_archive' => $has_archive,
        'rewrite' => array('slug' => $slug , 'with_front' => false),
        'exclude_from_search' => ! $search,
        'supports' => $supports,
        'taxonomies' => $taxonomies
    );

  register_post_type( $cpt_name, $args );
}

1 个答案:

答案 0 :(得分:0)

我无法弄清楚资源自定义帖子类型的其他位置在哪里注册,但我找到了解决我问题的黑客。我添加了一个取消注册功能,并将动作优先级设置为高于寄存器功能。我更新了我的代码以反映解决方案。我知道这不是最佳做法,但我不想花费太多时间去尝试找出代码中其他位置创建资源自定义帖子类型的位置。