[Edited]
我把网站的目的改成了语言学习网站。
现在,CPT设置如下:
{ Language } - { Subject }
<英语口语(slug:英语口语)Single CPT setup
因为每个CPT可能有不同的分类法……所以最好考虑代码。假设:CPT“英语口语”有一个自定义的“口语任务”(口语任务)分类法,术语为“任务1”、“任务2”、“任务3”。但是:CPT“英语写作”根本没有分类学。
单个CPT post项目所需的URL:
With custom taxonomy terms:
- www.domain。com/{language}/{subject}/{term slug}/{custom question id}/{post name}
- 例如www.domain。com/英语/口语/口语-1/32/诸如此类诸如此类
Without custom taxonomy terms:
- www.domain。com/{语言}/{主题}/{自定义问题id}/{帖子名}
- 例如www.domain。com/英语/写作/32/诸如此类诸如此类
The URL rewrite part for Single CPT is not a problem for me now thanks to your teaching and genius code provided in the new answer
我正在努力重写下面的CPT归档页面:<小时>
CPT Archive setup
我正在尝试使用以下URL设置创建自定义CPT存档模板:www.domain。com/{语言}/{主题}例如www.domain。com/英语/口语
www.domain。com/{语言}/{主题}/{术语段塞}
示例1。www.domain。com/english/speaking(不是列出所有帖子,而是将其默认设置为“speaking-1”)
- ex2。www.domain。com/english/speaking/speaking-1(仅在“speaking-1”下列出帖子)示例3。www.domain。com/english/speaking/speaking-2(仅在“speaking-2”下列出帖子)
在中[archive-english-speaking.php] , 我有以下代码:
-----------------------------------// use add_rewrite_rule in funcitons; query_vars "task" is terms of speaking-task taxonomy global $wp_query; $task_no = $wp_query->query_vars[\'task\']; if (preg_match("/[1-6]/", $task_no)) { $task = ( \'speaking-\' . $task_no ); } else { $task_no = ( \'1\' ); $task = ( \'speaking-\' . $task_no ); } $posts_per_page = 10; $the_query = new WP_Query( array( \'post_type\' => \'english-speaking\', \'taxonomy\' => \'speaking-task\', \'term\' => $task, /*\'paged\' => $paged, \'posts_per_page\' => $posts_per_page,*/ \'orderby\' => \'meta_value_num\', \'meta_key\' => \'wpcf-question-id\', \'order\' => \'ASC\', /* ASC/DESC */ ) );
在中[functions.php] , 我有以下代码:
-----------------------------------function custom_rewrite_tag() { //taxonomy speaking-task =? add_rewrite_tag(\'%task%\', \'([^/]*)\'); } add_action(\'init\', \'custom_rewrite_tag\', 10, 0); // Custom Question ID from custom field function cqid() { $cqid = get_post_meta( get_the_ID(), \'wpcf-question-id\', true ); return $cqid; } function my_questions_cpt_list() { return [ // Format: POST_TYPE => QUESTION_SUBJECT_SLUG \'english-listening\' => \'listening\', \'english-speaking\' => \'speaking\', \'english-reading\' => \'reading\', \'english-writing\' => \'writing\', // Add more items as needed, or if and when you register another CPT // for questions. ]; } add_action( \'init\', \'my_questions_cpt_add_rewrite_rules\' ); function my_questions_cpt_add_rewrite_rules() { // Archive Page add_rewrite_rule( \'^english/speaking/?$\',\'index.php?post_type=english-speaking&taxonomy=speaking-task&task=$matches[1]\', \'top\' ); add_rewrite_rule( \'^english/speaking/([^/]*)/?$\',\'index.php?post_type=english-speaking&taxonomy=speaking-task&task=$matches[1]\', \'top\' ); // $cpt_list = my_questions_cpt_list(); foreach ( $cpt_list as $post_type => $q_subject ) { if ( empty( $post_type ) ) { continue; } if ( ! $s = preg_quote( $q_subject ) ) { continue; } add_rewrite_rule(\'^english/(\' . $s . \')/(\\d+)/(\\d+)/([^/]+)/?\',\'index.php?\' . $post_type . \'=$matches[4]\',\'top\'); } } add_filter( \'post_type_link\', \'my_questions_cpt_custom_permalink\', 10, 2 ); function my_questions_cpt_custom_permalink( $permalink, $post ) { $taxonomy = \'speaking-task\'; $cpt_list = my_questions_cpt_list(); if ( false === strpos( $permalink, \'?\' ) && $post && isset( $cpt_list[ $post->post_type ] ) ) { $cats = get_the_terms( $post, $taxonomy ); if ( $cats && ! is_wp_error( $cats ) ) { // If assigned to multiple tasks (or categories), then we use just // the first task/term. $cat_id = $cats[0]->slug; //term_id $task = $cat_id[strlen($cat_id)-1]; $cqid = cqid(); $s = $cpt_list[ $post->post_type ]; $permalink = home_url( \'english/\' . $s . \'/\' . $task . \'/\' . $cqid . \'/\' . $post->post_name . \'/\' ); $permalink = user_trailingslashit( $permalink, \'single\' ); } } return $permalink; }
@Sallyth以上代码仅限于我的编程级别,我使用了您的第一个答案并对其进行了修改。。。这是一种工作在本地主机,但存档url得到404或空白页时,我把它移动到在线托管服务器。。。