我目前正在建设一个网站,并已收到一个定制URL struct 显示的请求.以下是我目前的设置:
- 在wp—admin仪表板中,一般的permalinks struct 设置设置为'Post name'.
- 我创建了一个自定义的帖子类型,名为"information",代码如下:
function create_custom_post_type_information() {
$labels = array(
'name' => __( 'お知らせ一覧' ),
'singular_name' => __( 'お知らせ一覧' ),
'menu_name' => __( 'お知らせ一覧' ),
'add_new' => __( '新規追加' ),
'add_new_item' => __( '新しいお知らせ一覧を追加' ),
'edit' => __( '編集' ),
'edit_item' => __( 'お知らせ一覧の編集' ),
'new_item' => __( '新しいお知らせ一覧' ),
'view' => __( '表示' ),
'view_item' => __( 'お知らせ一覧を表示' ),
'search_items' => __( 'お知らせ一覧を検索' ),
'not_found' => __( 'お知らせ一覧が見つかりません' ),
'not_found_in_trash' => __( 'ゴミ箱にお知らせ一覧が見つかりません' ),
);
$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => false,
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments', 'revisions' ),
'rewrite' => array( 'slug' => 'information'),
'show_in_rest' => true,
);
register_post_type( 'information', $args );
}
add_action( 'init', 'create_custom_post_type_information' );
// カスタムタクソノミー:お知らせ一覧のカテゴリー / information_category
function create_custom_taxonomy_information_category() {
$labels = array(
'name' => __( 'お知らせ一覧カテゴリー' ),
'singular_name' => __( 'お知らせ一覧カテゴリー' ),
'search_items' => __( 'お知らせ一覧カテゴリーを検索' ),
'all_items' => __( 'すべてのお知らせ一覧カテゴリー' ),
'parent_item' => __( '親カテゴリー' ),
'parent_item_colon' => __( '親カテゴリー:' ),
'edit_item' => __( 'お知らせ一覧カテゴリーを編集' ),
'update_item' => __( 'お知らせ一覧カテゴリーを更新' ),
'add_new_item' => __( 'お知らせ一覧カテゴリーを追加' ),
'new_item_name' => __( '新しいお知らせ一覧カテゴリー' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'info-ctg' ),
'show_in_rest' => true,
);
register_taxonomy( 'info_ctg', array( 'information' ), $args );
}
add_action( 'init', 'create_custom_taxonomy_information_category' );
- 我还创建了一个名为"信息"的新页面.因此,我设置了
has_archive' => false,
并使用以下代码处理分页:
add_rewrite_rule( 'information/page/([0-9]+)/?$', 'index.php?pagename=information&paged=$matches[1]', 'top' );
个
到目前为止,我所做的工作是有效的,但现在我困惑于如何定制只有这篇文章的格式如下:
/information/yyyyy/mm/dd/post_id
我试过以下代码,但它没有工作,并显示了一个404页面:
function custom_permalink_structure($permalink, $post) {
// For single information posts
if ($post->post_type === 'information') {
$permalink = home_url('/information/' . date('Y/m/d', strtotime($post->post_date)) . '/' . $post->ID);
}
return $permalink;
}
add_filter('post_type_link', 'custom_permalink_structure', 10, 2);
我一直在跟踪这个https://wordpress.stackexchange.com/a/22490,但它没有工作.
有谁能帮我定制一下这个 struct 吗?谢谢.