可以将另一个设置添加到自定义发布类型的“首页显示”设置中

时间:2011-11-02 作者:Scott

让我来设置场景:

我有一个CPT将在多站点安装上工作,其中一些站点将使用不同的语言。例如,我的CPT是Case Studies 我会使用如下URL结构:/case-studies/%post_id%/%postname%/ 但如果网站语言不同case-studies 不再是那样了。

在WP设置中,您可以将页面设置为您的帖子页面。这基本上设置了front 到您的permalink结构。我想做的是为我的CPT添加一个额外的下拉列表,允许为该CPT的帖子设置首页。因此,如果语言不同,则permalink结构可以与该语言匹配。

Add setting here

那么,是否可以在上图所示的位置使用设置API扩展设置?如果是,那么我该怎么做?如果没有,那么哪里是放置我的CPT设置的合适位置?(如果您可以提供一些代码示例,则可以获得额外的积分)

2 个回复
最合适的回答,由SO网友:Scott 整理而成

我采用了以下方法:

我已经有了一个使用设置API的主题选项页面,因为我设置的这个CPT是主题的一部分,所以只有将这个选项添加到主题选项中才有意义。以下是任何人想要使用的代码:

<?php
// Add settings to menu
add_action( \'admin_menu\', \'sc_hi_rezz_theme_options_add_page\' );
function sc_hi_rezz_theme_options_add_page() {
    add_theme_page(
        __( \'Theme Options\', \'hi-rezz\' ),
        __( \'Theme Options\', \'hi-rezz\' ),
        hi_rezz_get_options_page_cap(),
        \'theme_options\',
        \'sc_hi_rezz_theme_options_do_page\'
    );
}


// see https://make.wordpress.org/themes/2011/07/01/wordpress-3-2-fixing-the-edit_theme_optionsmanage_options-bug/
function hi_rezz_get_options_page_cap() {
    return \'edit_theme_options\';
}
add_filter( \'option_page_capability_hi-rezz-options\', \'hi_rezz_get_options_page_cap\' );


// Fix Cap to allow editors to edit theme options
function sc_hi_rezz_get_options_page_cap() {
    return \'edit_theme_options\';
}
add_filter( \'option_page_capability_hi-rezz-options\', \'sc_hi_rezz_get_options_page_cap\' );


// Options Page Layout
function sc_hi_rezz_theme_options_do_page() {
    ?>
    <div class="wrap">
        <?php screen_icon(); ?>
        <h2><?php _e( \'Theme Options\', \'hi-rezz\' ) ?></h2>
        <form action="options.php" method="post">
            <?php
            settings_fields( "hi_rezz_theme_options" );
            do_settings_sections( "theme_options" );
            ?>
            <?php submit_button( __( "Save changes", "hi-rezz" ), "primary", "submit", true ); ?>
        </form>
    </div>
    <?php
}


// Register Settings
add_action( \'admin_init\', \'sc_hi_rezz_theme_settings_register\' );
function sc_hi_rezz_theme_settings_register() {
    register_setting("hi_rezz_theme_options", "hi_rezz_theme_options", "sc_hi_rezz_theme_settings_validate"); // Register Main Settings
    add_settings_section("sc_hi-rezz_text", __("Text / Copy", "hi-rezz"), "hi_rezz_theme_settings_dummy", "theme_options"); // Make settings text section
    add_settings_section("sc_hi-rezz_cpt_settings", __("Custom Post Type Settings", "hi-rezz"), "hi_rezz_theme_settings_dummy", "theme_options"); // Make settings text section
    add_settings_field("hi_rezz_theme_settings_footer_copy", __("Footer copy", "hi-rezz"), "hi_rezz_theme_settings_footer_copy_field", "theme_options", "sc_hi-rezz_text");
    add_settings_field("hi_rezz_theme_settings_cpt_cs_page", __("Page for case studies", "hi-rezz"), "hi_rezz_theme_settings_cpt_cs_field", "theme_options", "sc_hi-rezz_cpt_settings");
}
function hi_rezz_theme_settings_dummy() { }


// Validate Settings
function sc_hi_rezz_theme_settings_validate($input) {
    $valid = get_option("hi_rezz_theme_options");
    $valid[\'sc_footer_copy\'] = wp_filter_post_kses( $input[\'sc_footer_copy\'] );
    $valid[\'sc_cpt_cas_study_page\'] = (int) $input[\'sc_cpt_cas_study_page\'];
    return $valid;
}


//Settings Fields
function hi_rezz_theme_settings_footer_copy_field() {
    $options = get_option("hi_rezz_theme_options");
    ?><textarea rows="4" cols="50" name="hi_rezz_theme_options[sc_footer_copy]" id="hi_rezz_theme_settings_footer_copy"><?php echo esc_textarea($options[\'sc_footer_copy\']); ?></textarea><?php
}
function hi_rezz_theme_settings_cpt_cs_field() {
    $options = get_option("hi_rezz_theme_options");
    wp_dropdown_pages(
        array(
             \'name\' => \'hi_rezz_theme_options[sc_cpt_cas_study_page]\',
             \'echo\' => 1,
             \'show_option_none\' => __( \'&mdash; Select &mdash;\' ),
             \'option_none_value\' => \'0\',
             \'selected\' => $options[\'sc_cpt_cas_study_page\']
        )
    );
}
?>
以上给了我:

My CPT settings

SO网友:Chip Bennett

看来options-reading.php file is hard-coding its options, 而不是使用设置API。

该选项正在使用wp_dropdown_pages() 明确地法典列出了以下参数wp_dropdown_pages():

<?php 
$args = array(
    \'depth\'            => 0,
    \'child_of\'         => 0,
    \'selected\'         => 0,
    \'echo\'             => 1,
    \'name\'             => \'page_id\'); 
?>
Codex页面还指出,该函数理论上可以接受任何可以传递给的参数get_pages(), 其中包括post_type 参数:

<?php 
$args = array(
    \'child_of\'     => 0,
    \'sort_order\'   => \'ASC\',
    \'sort_column\'  => \'post_title\',
    \'hierarchical\' => 1,
    \'exclude\'      => ,
    \'include\'      => ,
    \'meta_key\'     => ,
    \'meta_value\'   => ,
    \'authors\'      => ,
    \'exclude_tree\' => ,
    \'post_type\' => \'page\',
?>
。。。这意味着理论上wp_dropdown_menu() 可以修改以返回自定义帖子类型的帖子。

请注意wp_dropdown_pages() 是否有an output filter hook, wp_dropdown_pages:

$output = apply_filters(\'wp_dropdown_pages\', $output);
。。。所以也许你可以用过滤器来定位page_on_front 具体选择表单字段:

wp_dropdown_pages( array( \'name\' => \'page_on_front\', \'echo\' => 0, \'show_option_none\' => __( \'&mdash; Select &mdash;\' ), \'option_none_value\' => \'0\', \'selected\' => get_option( \'page_on_front\' ) ) ) );

EDIT

根据此评论:

我想做的是为我的CPT设置一个页面作为posts页面,就像您可以为posts做的一样

我仍然不确定核心设置是否真的可以以这种方式扩展,但您确实有其他选择。

最明显的是创建custom Page template 那是一个"page for Posts", in which you query your specific Custom Post Type.

主要是从抄本链接复制意大利面;您需要修改标记以适应您的主题:

<?php
/**
 * Template Name: Case Studies
 */
?>

<?php get_header(); ?>

<div id="content">

<?php 
$type = \'case-studies\'; // Use the correct CPT slug here
$args=array(
  \'post_type\' => $type,
  \'post_status\' => \'publish\',
  \'paged\' => $paged,
  \'caller_get_posts\'=> 1 // do not show sticky posts
);
// This bit is a "hack" to allow pagination to work properly
$temp = $wp_query;    
$wp_query = null;
$wp_query = new WP_Query($args); 
?>

<?php

 get_template_part( \'loop\', \'index\' );
 // Restore the original $wp_query
 $wp_query = $temp
?>

</div><!-- #content -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
然后,当然,只需创建一个页面,并将其指定为“案例研究”模板。

结束