创建以图像为中心的自定义帖子类型?

时间:2010-08-20 作者:Annika Backstrom

有没有人有任何创建以图像为中心的自定义帖子类型的技巧?

更详细地说,我的博客有旋转标题图像,如下所示:

Blog header showing random images

左上角的两个图像是随机的,并且作为附件存在于仅包含这些图像的特定页面。我想知道使用自定义帖子类型以不同的方式存储这些内容是否可行。我已经创建了一个新的帖子类型,“标题图片”,我正试图找出从这里开始的方向。我想每个标题图像“后”有一个图像附件。我不会从页面中随机抽取图像,而是从标题图像帖子类型中随机抽取帖子。鉴于此,

如何将一个简单的界面合并到“新标题图像”管理页面中提供的附件过程中

  • 我可以删除帖子标题和内容输入框来整理页面吗create a better interface 到当前的上载过程,并最终能够create a taxonomy 将图像标记为左/右图片。(查看上图,您可以看到右侧的照片遮盖了另一张照片中的面部。我可以通过将照片标记为左侧和/或右侧显示来避免这种情况。)如果我能实现前者,后者就不会成为问题。

    Update: 根据这里的答案,我能够实现这个设置。完整代码是posted below.

  • 2 个回复
    SO网友:Annika Backstrom

    goldenapple最初的回答给了我完成这项工作所需的快速启动。

    功能。php下面是我用来添加新帖子类型“header image”并相应修改其他管理屏幕的完整代码:

    /**
     * Register the Header Image custom post type.
     */
    function sixohthree_init() {
        $labels = array(
            \'name\' => \'Header Images\',
            \'singular_name\' => \'Header Image\',
            \'add_new_item\' => \'Add Header Image\',
            \'edit_item\' => \'Edit Header Image\',
            \'new_item\' => \'New Header Image\',
            \'view_item\' => \'View Header Image\',
            \'search_items\' => \'Search Header Images\',
            \'not_found\' => \'No Header Images found\',
            \'not_found_in_trash\' => \'No Header Images found in Trash\'
        );
    
        $args = array(
            \'labels\' => $labels,
            \'public\' => false,
            \'show_ui\' => true,
            \'supports\' => array(\'thumbnail\')
        );
    
        register_post_type( \'header-image\', $args );
    }
    add_action( \'init\', \'sixohthree_init\' );
    
    /**
     * Modify which columns display when the admin views a list of header-image posts.
     */
    function sixohthree_headerimage_posts_columns( $posts_columns ) {
        $tmp = array();
    
        foreach( $posts_columns as $key => $value ) {
            if( $key == \'title\' ) {
                $tmp[\'header-image\'] = \'Header Image\';
            } else {
                $tmp[$key] = $value;
            }
        }
    
        return $tmp;
    }
    add_filter( \'manage_header-image_posts_columns\', \'sixohthree_headerimage_posts_columns\' );
    
    /**
     * Custom column output when admin is view the header-image post list.
     */
    function sixohthree_headerimage_custom_column( $column_name ) {
        global $post;
    
        if( $column_name == \'header-image\' ) {
            echo "<a href=\'", get_edit_post_link( $post->ID ), "\'>", get_the_post_thumbnail( $post->ID ), "</a>";
        }
    }
    add_action( \'manage_posts_custom_column\', \'sixohthree_headerimage_custom_column\' );
    
    /**
     * Make the "Featured Image" metabox front and center when editing a header-image post.
     */
    function sixohthree_headerimage_metaboxes( $post ) {
        global $wp_meta_boxes;
    
        remove_meta_box(\'postimagediv\', \'header-image\', \'side\');
        add_meta_box(\'postimagediv\', __(\'Featured Image\'), \'post_thumbnail_meta_box\', \'header-image\', \'normal\', \'high\');
    }
    add_action( \'add_meta_boxes_header-image\', \'sixohthree_headerimage_metaboxes\' );
    
    /**
     * Enable thumbnail support in the theme, and set the thumbnail size.
     */
    function sixohthree_after_setup() {
        add_theme_support( \'post-thumbnails\' );
        set_post_thumbnail_size(150, 100, true);
    }
    add_action( \'after_setup_theme\', \'sixohthree_after_setup\' );
    
    管理屏幕截图

    Header Images post list

    Header Images post editing

    模板代码

    $header_images = get_posts(\'post_type=header-image&orderby=rand&numberposts=2\');
    
    foreach( $header_images as $idx => $post ) {
        setup_postdata($post);
        the_post_thumbnail(\'post-thumbnail\', array(\'class\' => \'snapshot snapshot\' . ($idx+1) ) );
    }
    

    SO网友:goldenapples

    function register_header_image() {
         register_post_type( \'header-image\', 
                             array( 
                                 \'label\'=>\'Header Images\',
                                 \'name\'=>\'Header Images\',
                                 \'singular_name\'=>\'Header Image\',
                                 \'public\'=>true,
                                 \'show_ui\'=>true,
                                 \'hierarchical\'=>true,
                                 \'supports\'=>array(\'thumbnail\') ) );
    }
    
    add_action (\'init\',\'register_header_image\');
    add_theme_support( \'post-thumbnails\' );
    
    这应该注册你的帖子类型,除了一个特色图片的字段。参见法典http://codex.wordpress.org/Function_Reference/register_post_type 获取要传递的参数列表。

    结束

    相关推荐