自定义帖子类型固定链接每隔一次保存就会更改

时间:2019-04-25 11:33:52

标签: php regex wordpress permalinks

我创建了一个父代>子代>孙子结构。 (课程>系列>剧集)。

我创建了以下代码,用于更新每种自定义帖子类型的URL,以便可以使用URL结构

http://mywebsite.co.uk/system/%course%/%series%/%episode%

例如,一门名为“速成课程”的课程,其中包含一系列“基础知识”,并带有一集“检查镜子”,看起来像这样

http://mywebsite.co.uk/system/crash-course/the-basics/checking-your-mirrors

一切正常,但我注意到一个非常奇怪的错误。每当我保存一个情节时,固定链接都会更改以尝试匹配随机剧集?

例如,如果我保存一次情节,我的固定链接就是

http://mywebsite.co.uk/system/crash-course/the-basics/checking-your-mirrors

然后,当我回到情节中时,进行任何更改并尝试再次保存它,一旦我保存并刷新页面,它就会自动更改为

http://mywebsite.co.uk/system/crash-course/the-basics/the-basics-2,我必须单击“编辑”并手动将其更改回。

下面是我的完整自定义帖子类型注册代码,如果有人有任何想法,请告诉我!每当我进行更改时,手动重新更改永久链接是不理想的!

注意-我为以下巨大的代码片段表示歉意,我认为最好粘贴所有自定义帖子类型代码以帮助调试!

<?php
/* ----------------------------------------------------------------------------------------------------------
        Register our custom post types
---------------------------------------------------------------------------------------------------------- */
function create_post_type() {
    //Courses
    register_post_type( 'course',
      [
        'labels' => array(
          'name' => __( 'Courses' ),
          'singular_name' => __( 'Course' )
        ),
        'description' => 'All courses',
        'public' => true,
        'hierarchical' => true,
        'rewrite' => array(
            'slug'       => 'system',
        ),
        'menu_icon' => 'dashicons-welcome-learn-more',
        'supports' => ['title', 'custom_fields', 'page-attributes']
      ]
    );

    //Series
    register_post_type( 'series',
      [
        'labels' => array(
          'name' => __( 'Series' ),
          'singular_name' => __( 'Series' )
        ),
        'description' => 'Course Series',
        'public' => true,
        'hierarchical' => false,
        'show_ui'              => true,
        'show_in_menu'         => 'edit.php?post_type=course',
        'supports' => ['title', 'custom_fields', 'page-attributes']
      ]
    );

    //Episodes
    register_post_type( 'episodes',
      [
        'labels' => array(
          'name' => __( 'Episodes' ),
          'singular_name' => __( 'Episode' )
        ),
        'description' => 'Series Episodes',
        'public' => true,
        'hierarchical' => false,
        'show_ui'              => true,
        'show_in_menu'         => 'edit.php?post_type=course',
        'supports' => ['title', 'custom_fields', 'page-attributes']
      ]
    );
}
add_action( 'init', 'create_post_type' );



/* ----------------------------------------------------------------------------------------------------------
        Add our meta boxes to our series and episode add/edit page
---------------------------------------------------------------------------------------------------------- */
function my_add_meta_boxes() {
  //Series Meta Boxes
  add_meta_box( 'series-parent', 'Course', 'series_attributes_meta_box', 'series', 'side', 'high' );

  //Episode Meta Boxes
  add_meta_box( 'episode-parent', 'Series', 'episode_attributes_meta_box', 'episodes', 'side', 'high' );
}
add_action( 'add_meta_boxes', 'my_add_meta_boxes' );

function series_attributes_meta_box($post) {
  $post_type_object = get_post_type_object($post->post_type);
    $pages = wp_dropdown_pages([
    'post_type' => 'course',
    'selected' => $post->post_parent,
    'name' => 'parent_id',
    'show_option_none' => __( '(no parent)' ),
    'sort_column'=> 'menu_order, post_title',
    'echo' => 0
  ]);
  if (!empty($pages)){
        echo $pages;
    }
}

function episode_attributes_meta_box( $post ) {
  $post_type_object = get_post_type_object( $post->post_type );
  $select = "<select name='parent_id' id='parent_id'>";
    $select .= "<option value=''>(No Parent)</option>";
    //Get Series
    $args = [
      'post_type' => 'series',
      'posts_per_page' => -1,
      'meta_key' => 'series_number',
      'orderby' => 'meta_value_num',
      'order' => 'ASC'
    ];
    $seriesQuery = new WP_Query($args);
    //Loop the series
    while($seriesQuery->have_posts()){
      $seriesQuery->the_post();
      $select .= "<option " . (get_the_ID() == $post->post_parent ? 'selected' : '') . " class='level-0' value='" . get_the_ID() . "'>" . get_the_title(wp_get_post_parent_id(get_the_ID())) . ' - ' . get_the_title() . "</option>";
    }
    wp_reset_postdata();
  $select .= "</select>";
  echo $select;
}


/* ----------------------------------------------------------------------------------------------------------
        String replace on the permalinks before we save them
---------------------------------------------------------------------------------------------------------- */
function custom_permalinks($permalink, $post, $leavename) {

  $post_id = $post->ID;
  $parent = $post->post_parent;
  $parent_post = get_post( $parent );

  //Series or Episodes
  switch($post->post_type){
    case 'episodes':
        if(empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft'))){
          return $permalink;
        } else {
          $grandparent_post = get_post( $parent_post->post_parent );
          $search = ['%course%', '%series%'];
          $replace = [$grandparent_post->post_name, $parent_post->post_name];
          $permalink = str_replace($search, $replace, $permalink);
          return $permalink;
        }
    break;
    case 'series':
        if(empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft'))){
          return $permalink;
        } else {
          $search = ['%course%'];
          $replace = [$parent_post->post_name];
          $permalink = str_replace($search, $replace, $permalink);
          return $permalink;
        }
    break;
    default:
        return $permalink;
    break;
  }

}
add_filter('post_type_link', 'custom_permalinks', 10, 3);

function my_add_rewrite_rules() {

  //Episodes
  add_permastruct('episodes', '/system/%course%/%series%/%episodes%', false,  ['walk_dirs' => false]);
  add_rewrite_tag('%episodes%', '([^/]+)', 'episodes=');
  add_rewrite_rule('^system/([^/]+)/([^/]+)/([^/]+)?','index.php?episodes=$matches[3]','top');

  //Series
  add_permastruct('series', '/system/%course%/%series%', false,  ['walk_dirs' => false]);
    add_rewrite_tag('%series%', '([^/]+)', 'series=');
  add_rewrite_rule('^system/([^/]+)/([^/]+)/?','index.php?series=$matches[2]','top');


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

?>

0 个答案:

没有答案