繁体   English   中英

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

[英]Custom Post Type equivalent for “Posts Page” setting in Wordpress

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

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

基于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 );
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM