带下拉选项的单一帖子模板?

时间:2013-04-12 作者:AndrettiMilas

我希望我的主题的用户能够通过帖子屏幕上的下拉菜单选择一个帖子模板。

我知道a plugin 这实现了这一点,但我希望能够实现同样的事情没有插件。有人有办法吗?

1 个回复
SO网友:Zifana Safira

`

        if (is_singular()) {
            $zifana_captain_hook = get_page_template();

            if ($zifana_captain_hook && !preg_match(\'/page.php$/\', $zifana_captain_hook)) {
                include($zifana_captain_hook);
                die();
            }

        }

    }

    // -- Function Name : save_post
    // -- Params : $post_id, $post
    // -- Purpose : 
    function save_post($post_id, $post) {
        $zifana_captain_hook = isset($_POST[\'zifana_theme_styling\'])? $_POST[\'zifana_theme_styling\'] :
        \'\';

        if ( !empty($zifana_captain_hook) ) {
            $zifana_captain_hooks = get_page_templates();
            $post->page_template = $zifana_captain_hook;

            if ( \'default\' != $zifana_captain_hook && !in_array($zifana_captain_hook, $zifana_captain_hooks) ) {
                return;
            }

            update_post_meta($post_id, \'_wp_page_template\', $zifana_captain_hook);
        }

    }

    // -- Function Name : zifana_hook_theme_metabox
    // -- Params : $post
    // -- Purpose : 
    function zifana_hook_theme_metabox($post) {
        $zifana_captain_hook = get_post_meta( $post->ID, \'_wp_page_template\', true );
        ?>
    // -- Function Name : add_dropdown
    // -- Params : 
    // -- Purpose : 
    function add_dropdown() {

        if ( 0 != count( get_page_templates() ) ) {
            add_meta_box(\'themezifanastyle\',__( \'What template huh..\', \'zifana\'),array(&$this, \'zifana_hook_theme_metabox\'),\'post\');
        }

    }

};
$__zifana_theme_stylings = & new zifanaTemplate;

if (is_admin()) {
    add_action(\'admin_menu\', array(&$__zifana_theme_stylings, \'add_dropdown\'));
}

add_action(\'zifana_page_hook\', array(&$__zifana_theme_stylings, \'zifana_page_hook\'));
add_action(\'save_post\', array(&$__zifana_theme_stylings, \'save_post\'), 10, 2);
“我希望能帮上忙……”

结束

相关推荐

Multiple Page Templates & CSS

在使用多个页面模板的主题上处理CSS的最佳实践是什么?例如:我将有一个全宽页面模板和一个两列页面模板。最好只在单个CSS中调用容器(#fullWidthContainer vs.#twoColumnContainer)还是为每个页面模板提供单独的CSS文件更好?