自定义帖子类型设置页面,选择页面以显示存档


12

因此,我遵循了该帖子的公认答案。可以为“自定义帖子类型”的“首页显示”设置添加其他设置

但并非完全有效,因此必须与本文中已接受的答案进行比较。

无法输出do_settings_sections。不明白为什么

现在,它实际上使用以下代码工作

//Add settings to menu
add_action( 'admin_menu', 'events_options_add_page' );
function events_options_add_page() {
//  add_submenu_page('edit.php?post_type=events', 'Events Admin', 'Events Settings', 'edit_posts', basename(__FILE__), 'events_options_do_page');
    add_submenu_page('edit.php?post_type=events', 'Events Admin', 'Events Settings', 'edit_posts', basename(__FILE__), 'events_options_do_page');
}
///wordpress/32689/possible-to-add-another-setting-to-front-page-displays-setting-for-custom-post


//Fix Cap to allow editors to edit theme options
function events_get_options_page_cap() {
    return 'edit_posts';    
}
add_filter('option_page_capability_events-options', 'events_get_options_page_cap' );


function events_options_do_page() {
?>
    <div class="wrap">
        <?php screen_icon(); ?>
        <h2><?php _e( 'Events Options', 'FoundationPress' ); ?></h2>
        <form action="options.php" method="post">
            <?php
            settings_fields( "events_settings_options" );
            do_settings_sections( "edit_posts" );
            ?>
            <?php submit_button( __( "Save changes", "FoundationPress" ), "primary", "submit", true ); ?>
        </form>
    </div>
    <?php
}

//Register Settings
add_action( 'admin_init', 'events_settings_register' );

function events_settings_register() {
    register_setting( "events_settings_options", "events_settings_options", "events_settings_validate" ); //Register main settings
//  add_settings_section( "events_cpt_settings", __( "Events Page Settings", "FoundationPress" ), "events_settings_dummy", "edit_posts"  ); //Make settings text section
//  add_settings_field( "events_settings_cpt_page", __( "Page for events", "FoundationPress" ), "events_settings_page_select", "edit_posts", "events_settings_options" );
    add_settings_section( "events_cpt_settings", __( "Events Page Settings", "FoundationPress" ), "events_settings_dummy", "edit_posts"  ); //Make settings text section
    add_settings_field( "events_settings_cpt_page", __( "Page for events", "FoundationPress" ), "events_settings_page_select", "edit_posts", "events_cpt_settings" );
}

function events_settings_dummy() {}

//Validate Settings
function events_settings_validate ($input) {
    $valid = get_option ( "events_settings_options" );
    $valid['events_page'] = (int) $input['events_page'];
    return $valid;  
}

//Settings Fields
function events_settings_page_select() {
    $options = get_option( 'events_settings_options' );
    wp_dropdown_pages(
        array(
            'name' => 'events_settings_options[events_page]',
            'echo' => 1,
            'show_option_none' => __( '&mdash; Select &mdash;' ),
            'option_none_value' => '0',
            'selected' => $options['events_page']
        )
    ); 
}

为了使其正常工作,我注意到了两个示例之间的差异,并将do_settings_sections更改为edit_posts ...现在我想到的目的是为了获得权限或功能,我尝试了其他方法,但只是想出了您没有权限等WordPress的。

任何人都可以帮助我理解我的代码吗?我转了一圈,非常感谢另一组注视着它的眼睛。

在此之后,我需要制作该页面,以便该页面实际上像在“阅读”页面中设置页面一样工作,以便加载我的自定义帖子类型档案。

非常感谢你们!

编辑:

不得不碰碰,迫切需要一些帮助!很高兴为贝宝啤酒解决问题!


有人可以帮忙吗?
卢克·瓦雷

它应该工作。我测试了您的代码。还是您需要其他帮助吗?
iantsch

在阅读您的问题时,我不确定您需要什么帮助,但这是一个允许您将页面模板
Howdy_McGee

您的目标仅仅是为自定义帖子类型提供自定义模板吗?
Slam

Answers:


1

有一种肮脏的方法(实际上是肮脏的)从自定义帖子类型设置页面将普通的WordPress页面作为存档页面进行附加。

您需要先取消注册帖子类型,然后重新创建。如果您不使用自定义参数,或者您足够勇敢,请尝试以下代码。

在设置页面的末尾添加此代码片段。

if(isset($_GET['settings-updated']) && $_GET['settings-updated'] == true){
    //Dirty as hell
    $esettings = get_option ( "events_settings_options" );
    $getslug = get_post_field('post_name',$esettings['events_page']);

    unregister_post_type('events');
    register_post_type('events',array(
        'public' => true,
        'rewrite' => array('slug'=>'events'),
        'has_archive' => $getslug,
    ));
    //Needed for automated rewrite rules
    flush_rewrite_rules();
    //To test the functionality
    echo get_post_type_archive_link( 'events' );
}

1
这让我在阅读时感到肮脏,但印象深刻。
MikeNGarrett
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.