自定义帖子类型等同于 Wordpress 中的“帖子页面”设置


Custom Post Type equivalent for "Posts Page" setting in Wordpress

我正在尝试在wordpress中设置一个自定义帖子类型插件,该插件允许选择一个页面,该页面将以与设置"帖子页面"设置相同的方式生成帖子列表,以便在访问页面的URL时生成自定义帖子类型存档页面而不是页面内容。

我可以选择要使用的页面进行

设置,它使用页面辅助信息域作为自定义帖子类型的重写辅助信息域,我只是无法弄清楚如何让它显示存档而不是页面内容。

基于WordPress模板层次结构,假设您使用的是Foo类型的自定义帖子,您希望将存档代码放在archive-foo.php中。

我有一个有效的解决方案,可能有一个更好的解决方案,但这适用于顶级页面。基本上,我检查页面的 slug 是否已更改,如果有,则刷新重写规则。

add_action( 'init', 'faqmgr_create_post_type' );
function faqmgr_create_post_type() {
    $options=get_option('faqmgr_options', array('faq_page'=>0, 'faq_slug'=>'faq'));
    $updated=false;
    $tpost=get_post( $options['faq_page'], OBJECT );
    if(is_null($tpost)){
        $options=array('faq_page'=>0, 'faq_slug'=>'faq');
    $updated=true;
        update_option('faqmgr_options', $options);
    }else{
        if($options['faq_slug']!=$tpost->post_name){
            $options['faq_slug']=$tpost->post_name;
            $updated=true;
        }
    }
    register_post_type( 'faq',
        array(
            'labels' => array(
                'name' => __( 'FAQs' ),
                'singular_name' => __( 'FAQ' ),
                'add_new_item' => __( 'Add FAQ' ),
                'edit_item' => __( 'Edit FAQ' ),
                'new_item' => __( 'New FAQ' ),
                'view_item' => __( 'View FAQ' ),
                'search_items' => __( 'Search FAQs' ) 
            ),
        'public' => true,
        'exclude_from_search'=>true,
        'has_archive' => true,
        'rewrite' => array(
            'slug' => $options['faq_slug'],
            'with_front' => false
        ),
        'supports' => array(
            'title',
            'editor',
            'thumbnail'
        )
        )
    );
    if($updated=true;){
        flush_rewrite_rules( false );
    }
}