我试图在一个名为“提问”的子页面上过滤查询,以列出用户最近提交的所有问题,但在进入该页面时,我得到了404。刷新permalink结构并没有解决该问题。当前设置为月和名称。
创建一个新问题可以正常工作,我可以访问新帖子。自定义分类法也可以正常工作,但归档页面不能正常工作。
这是我正在使用的代码。
Post type
function question_post_type() {
$labels = array(
\'name\' => _x( \'Questions\', \'Post Type General Name\', \'text_domain\' ),
\'singular_name\' => _x( \'Question\', \'Post Type Singular Name\', \'text_domain\' ),
\'menu_name\' => __( \'Questions\', \'text_domain\' ),
\'parent_item_colon\' => __( \'Parent Question:\', \'text_domain\' ),
\'all_items\' => __( \'All Questions\', \'text_domain\' ),
\'view_item\' => __( \'View Question\', \'text_domain\' ),
\'add_new_item\' => __( \'Add New Question\', \'text_domain\' ),
\'add_new\' => __( \'New Question\', \'text_domain\' ),
\'edit_item\' => __( \'Edit Question\', \'text_domain\' ),
\'update_item\' => __( \'Update Question\', \'text_domain\' ),
\'search_items\' => __( \'Search questions\', \'text_domain\' ),
\'not_found\' => __( \'No questions found\', \'text_domain\' ),
\'not_found_in_trash\' => __( \'No questions found in Trash\', \'text_domain\' ),
);
$rewrite = array(
\'slug\' => \'/ask/question\',
\'with_front\' => true,
\'pages\' => true,
\'feeds\' => true,
);
$args = array(
\'label\' => __( \'question\', \'text_domain\' ),
\'description\' => __( \'Question\', \'text_domain\' ),
\'labels\' => $labels,
\'supports\' => array( \'title\', \'editor\', \'author\', \'comments\', \'revisions\', \'custom-fields\', \'page-attributes\', ),
\'hierarchical\' => false,
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'show_in_nav_menus\' => true,
\'show_in_admin_bar\' => true,
\'menu_position\' => 20,
\'can_export\' => true,
\'has_archive\' => true,
\'exclude_from_search\' => false,
\'publicly_queryable\' => true,
\'rewrite\' => $rewrite,
\'capability_type\' => \'post\',
);
register_post_type( \'question\', $args );
}
add_action( \'init\', \'question_post_type\', 0 );
Taxonomy
if ( ! function_exists(\'custom_question_taxonomy\') ) {
// Register Custom Taxonomy
function custom_question_taxonomy() {
$labels = array(
\'name\'
=> _x( \'Question Types\', \'Taxonomy General Name\', \'text_domain\' ),
\'singular_name\' => _x( \'Question Type\', \'Taxonomy Singular Name\', \'text_domain\' ),
\'menu_name\' => __( \'Question Type\', \'text_domain\' ),
\'all_items\' => __( \'All Question Types\', \'text_domain\' ),
\'parent_item\' => __( \'Parent Question Type\', \'text_domain\' ),
\'parent_item_colon\' => __( \'Parent Question Type:\', \'text_domain\' ),
\'new_item_name\' => __( \'New Question Type\', \'text_domain\' ),
\'add_new_item\' => __( \'Add Question Type\', \'text_domain\' ),
\'edit_item\' => __( \'Edit Question Type\', \'text_domain\' ),
\'update_item\' => __( \'Update Question Type\', \'text_domain\' ),
\'separate_items_with_commas\' => __( \'Separate Question Types with commas\', \'text_domain\' ),
\'search_items\' => __( \'Search Question Type\', \'text_domain\' ),
\'add_or_remove_items\' => __( \'Add or remove Question Types\', \'text_domain\' ),
\'choose_from_most_used\' => __( \'Choose from the most used Question Types\', \'text_domain\' ),
);
$rewrite = array(
\'slug\' => \'question-type\',
\'with_front\' => false,
\'hierarchical\' => true,
);
$args = array(
\'labels\' => $labels,
\'hierarchical\' => true,
\'public\' => true,
\'show_ui\' => true,
\'show_admin_column\' => true,
\'show_in_nav_menus\' => true,
\'show_tagcloud\' => true,
\'rewrite\' => $rewrite,
);
register_taxonomy( \'question-type\', \'question\', $args );
}
add_action( \'init\', \'custom_question_taxonomy\', 0 );
}
Query
function question_posts_query( $query ) {
if ( $query->is_post_type_archive( \'question\' ) || $query->is_page(3001) && $query->is_main_query() ) {
$query->set( \'post_type\', \'question\' );
}
return $query;
}
add_filter( \'pre_get_posts\', \'question_posts_query\' );
My page loop
<?php if (have_posts()) :
while (have_posts() ) : the_post(); ?>
<div class="entry">
<div class="post-content">
<?php the_content(__(\'Read More »\', \'gp_lang\')); ?>
<?php the_terms( $post->ID, \'question-type\', \'<div class="category"><span>Category: </span>\', \'\', \'</div>\' ); ?>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>