【问题标题】:custom WordPress permalink structure not changed for all custom post types所有自定义帖子类型的自定义 WordPress 永久链接结构均未更改
【发布时间】:2018-10-21 04:45:36
【问题描述】:

我将 WordPress 网站中的默认永久链接结构更改为:www.example.com/blog/%postname%/。我希望 '/blog/' 部分仅适用于帖子。我有自定义帖子类型,例如“教育”、“案例研究”和“资源”,我不希望“博客”成为网址的一部分。我向 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 );
}

【问题讨论】:

  • 可以把注册CPT的代码贴出来吗?
  • @DanielJames 我添加了代码。
  • 我发现了部分问题。资源 cpt 实际上是在其他地方注册的。当我在上面的代码中注释掉resource的那行时,仍然出现了resource cpt。我不知道它在哪里注册。

标签: wordpress custom-post-type permalinks


【解决方案1】:

我无法确定资源自定义帖子类型的注册位置,但我找到了解决我的问题的 hack。我添加了一个取消注册功能并将操作优先级设置为高于注册功能。我更新了代码以反映解决方案。我知道这不是最佳做法,但我不想花太多时间试图找出在代码的其他位置创建资源自定义帖子类型的位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-10
    • 1970-01-01
    • 2022-01-01
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多