doulao3905 2012-11-29 01:42
浏览 59
已采纳

使用模板选择器(Page UI)创建自定义Wordpress帖子类型?

I know how to create custom post types. It seems from looking at the Codex that I should be able to create a custom post type that behaves like a page, specifically the ability to assign a template with the template chooser pop-up and category/tag pickers.

So far all I get is the basic editor, and I can get a featured image selector. But what I am looking for is basically a Page that I can treat as a custom post type.

EDIT: I thought it would be obvious, but I was doing it with functions.php:

register_post_type( hh_town,
array(
    'labels' => array(
        'name' => __( 'Towns' ),
        'singular_name' => __( 'Town' ),
        'add_new' => _x('Add Town', 'towns'),
              'add_new_item' => __('Add Town'),
              'edit' => _x('Edit Towns', 'Towns'),
              'edit_item' => __('Edit Town'),
              'new_item' => __('New Town'),
              'view' => _x('View Town', 'towns'),
              'view_item' => __('View Town')
    ),
    'public' => true,
    'has_archive' => true,
    'hierarchical' => true,
    'show_ui' => true,
    'supports' => array('title','editor','page-attributes','thumbnail', 'custom-fields'),
    'capability_type' => 'page',
    'taxonomies' => array('post_tag','category')
)
);
  • 写回答

1条回答 默认 最新

  • douzepao0281 2015-07-03 07:03
    关注

    Just add this code to your functions.php

    add_action( 'add_meta_boxes', 'add_custom_page_attributes_meta_box' );
    function add_custom_page_attributes_meta_box(){
    global $post;
        if ( 'page' != $post->post_type && post_type_supports($post->post_type, 'page-attributes') ) {
            add_meta_box( 'custompageparentdiv', __('Template'), 'custom_page_attributes_meta_box', NULL, 'side', 'core');
        }
    }
    
    function custom_page_attributes_meta_box($post) {
        $template = get_post_meta( $post->ID, '_wp_page_template', 1 ); ?>
        <select name="page_template" id="page_template">
            <?php $default_title = apply_filters( 'default_page_template_title',  __( 'Default Template' ), 'meta-box' ); ?>
            <option value="default"><?php echo esc_html( $default_title ); ?></option>
            <?php page_template_dropdown($template); ?>
        </select><?php
    }
    
    add_action( 'save_post', 'save_custom_page_attributes_meta_box' );
    function save_custom_page_attributes_meta_box( $post_id ) {
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
        if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) return;
        if ( ! current_user_can( 'edit_post', $post_id ) ) return;
        if ( ! empty( $_POST['page_template'] ) && get_post_type( $post_id ) != 'page' ) {
            update_post_meta( $post_id, '_wp_page_template', $_POST['page_template'] );
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。