Пользовательский тип поста Wordpress Постоянная ссылка с заголовком родительского поста в URL - PullRequest
0 голосов
/ 21 февраля 2019

Я только что создал пользовательский тип записи с именем 'mtl_chapter' .С помощью моего сценария я назначаю родительский пост моего CPT обычному типу 'post'.Итак, мой CPT в основном дети моих постоянных постов.Я хочу изменить структуру постоянных ссылок моего CPT с

/base-slug/cpt-post-title/ 

на

/parent-title/cpt-post-title/

Итак, это будет выглядеть так, как сообщения вложения делают с постоянной ссылкой:

/parent-title/attachment-post-title/

Мой текущий код может изменить структуру постоянной ссылки на то, что я хочу, но я получаю

404, не найденный

при нажатии на ссылку.Пожалуйста, помогите мне, вот мой текущий код:

function create_posttype() {
  register_post_type( 'mtl_chapter',
    array(
      'labels' => array(
        'name' => 'Chapters',
        'singular_name' => 'Chapter',
        'parent_item_colon' => 'Novel Title:',
        'add_new' => _x('Add New', 'indomtl'),
        'add_new_item' => __( 'Add New Chapter' )
      ),
      'public' => true,
      'has_archive' => true,
      'menu_icon' => 'dashicons-format-aside',
      'rewrite' => array('slug' => '%parent-post-name%','with_front' => true),
      'exclude_from_search' => true,
      'show_ui' => true,
      'menu_position' => 5
    )
  );
}

add_action( 'init', 'create_posttype' );

add_filter('post_type_link', 'mtl_update_permalink_structure', 10, 2);

function mtl_update_permalink_structure( $post_link, $post )
{
    if ( false !== strpos( $post_link, '%parent-post-name%' ) ) {
        $parent_id = wp_get_post_parent_id($post->ID);
        $parent_post = get_post($parent_id); 
        $slug = $parent_post->post_name;
        if ( $slug ) {
            $post_link = str_replace( '%parent-post-name%', $slug, $post_link );
        }

    }
    return $post_link;
}

1 Ответ

0 голосов
/ 21 февраля 2019

попробуйте использовать

function create_myposttype() {

    register_post_type( 'mtl_chapter',
    array(
      'labels' => array(
        'name' => 'Chapters',
        'singular_name' => 'Chapter',
        'parent_item_colon' => 'Novel Title:',
        'add_new' => _x('Add New', 'indomtl'),
        'add_new_item' => __( 'Add New Chapter' )
      ),
      'hierarchical' => true,
      'public' => true,
      'has_archive' => true,
      'menu_icon' => 'dashicons-format-aside',
      'rewrite' => array('slug' => 'mtl_chapter','with_front' => true),
      'exclude_from_search' => true,
      'show_ui' => true,
      'menu_position' => 5,
      'supports' => array(
            'page-attributes' /* This will show the post parent field */,
            'title',
            'editor',

        ),
    )
  );
}
add_action( 'init', 'create_myposttype' );
...