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

时间:2015-07-24 作者:Luke Wale

因此,我遵循了这篇帖子的公认答案Possible to add another setting to 'Front page displays' setting for Custom Post Type

但并不完全有效,必须与本帖中公认的答案进行比较。

Can't output do_settings_sections . Can't understand why

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

//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\');
}
//https://wordpress.stackexchange.com/questions/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\\u settings\\u部分更改为edit\\u posts。。。现在在我看来,它的目的是为了获得权限或功能,我已经尝试了其他一些技巧,但只是在wordpress中找到了你没有权限等。

谁能帮我理解一下我的代码吗?我已经绕了一圈,非常希望能有另一双眼睛看着它。

在此之后,我需要将其设置为页面实际工作方式与在阅读页面中设置页面一样,以便加载我的自定义帖子类型存档。

非常感谢各位!

编辑:

不得不颠簸,迫切需要一些帮助!很高兴在paypal上喝杯啤酒!

1 个回复
SO网友:Serkan Algur

有一种脏(实际上脏得要命)的方法可以从自定义帖子类型设置页面将普通WordPress页面附加为存档页面。

您需要先注销post类型,然后重新创建。如果您不使用自定义参数,或者您足够勇敢,请尝试此代码。

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

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\' );
}

结束

相关推荐

自定义帖子类型设置页面,选择页面以显示存档 - 小码农CODE - 行之有效找到问题解决它

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

时间:2015-07-24 作者:Luke Wale

因此,我遵循了这篇帖子的公认答案Possible to add another setting to 'Front page displays' setting for Custom Post Type

但并不完全有效,必须与本帖中公认的答案进行比较。

Can't output do_settings_sections . Can't understand why

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

//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\');
}
//https://wordpress.stackexchange.com/questions/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\\u settings\\u部分更改为edit\\u posts。。。现在在我看来,它的目的是为了获得权限或功能,我已经尝试了其他一些技巧,但只是在wordpress中找到了你没有权限等。

谁能帮我理解一下我的代码吗?我已经绕了一圈,非常希望能有另一双眼睛看着它。

在此之后,我需要将其设置为页面实际工作方式与在阅读页面中设置页面一样,以便加载我的自定义帖子类型存档。

非常感谢各位!

编辑:

不得不颠簸,迫切需要一些帮助!很高兴在paypal上喝杯啤酒!

1 个回复
SO网友:Serkan Algur

有一种脏(实际上脏得要命)的方法可以从自定义帖子类型设置页面将普通WordPress页面附加为存档页面。

您需要先注销post类型,然后重新创建。如果您不使用自定义参数,或者您足够勇敢,请尝试此代码。

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

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\' );
}

相关推荐