タグ: 未設定

クラス化したカスタム投稿タイプの一覧ページにthumbnailとカスタムタクソノミーを表示 :WordPress

投稿日 : 2013年2月17日 | 更新日 : 2013年02月17日 前のページへ戻る

Pocket

クラス化したカスタム投稿タイプの一覧ページにサムネイルとカスタムタクソノミー列を追加するメモ。

    /**
     * add column to costom  post type list
     *
     * @param string $key   field key custom post type filed name
     * @param string $label display label of field key
     */
    public function addTaxonomyColumn( $key, $label ) {

        $post_type_name = $this->post_type_name;

        // 一覧画面にカスタムタクソノミースタッフによる分類を追加
        $manage_edit_columns = function( $columns ) use( $key, $label )
        {
            /** @var string $key */
            /** @var string $label */
            $columns[ $key ] = $label;
            return $columns;
        };

        $manage_posts_column = function ( $column, $post_id ) use ( $key, $post_type_name )
        {
            /** @var string $key */
            /** @var string $post_type_name */
            if ( $key == $column ) {
                $value = get_the_term_list( $post_id, $post_type_name );
                $value = preg_replace( "/<[^>]+>(.+)<\/a>/", "$1", $value );
                echo $value;
            }
        };
        add_filter( 'manage_edit-' . $this->post_type_name . '_columns', $manage_edit_columns );
        add_action( 'manage_posts_custom_column', $manage_posts_column, 10, 2 );

    }

    public function addThumbnailColumn( $label )
    {
        $post_type_name = $this->post_type_name;
        // 一覧画面にカスタムタクソノミースタッフによる分類を追加
        $manage_edit_columns = function( $columns ) use( $label )
        {
            /** @var string $label */
            $columns[ 'thumbnail' ] = $label;
            return $columns;
        };
        $manage_posts_column = function ( $column, $post_id )
        {
            if ('thumbnail' == $column) {
                $value = get_the_post_thumbnail( $post_id, array( 100, 100 ) );
                echo $value;
            }
        };
        add_filter( 'manage_edit-' . $this->post_type_name . '_columns', $manage_edit_columns );
        add_action( 'manage_posts_custom_column', $manage_posts_column, 10, 2 );
    }

カスタムフィールド、カスタムタクソノミーを持つカスタム投稿タイプをクラス化する : WordPressにを追加したコード。

functions.php
include( get_template_directory() . ‘/inc/shortcode.php’ );

/*
 * custom post type
 */
$custom = new CustomPostType( 'custom', array() );
$custom->addTaxonomy( 'cat' );
$custom->addMetabox(
    'meta',
    array(
        'name' => 'text',
        'nationality' => 'text',
    )
);
$custom->addThumbnailColumn( 'eye catch' );
$custom->addTaxonomyColumn( 'cat', 'category' );
custom_post_type.php

custom_post_type.php

<?php
/**
 * CustomPostType class
 *
 * @author Sawai Hiroshi
 */

/**
 * CustomPostType
 */
class CustomPostType
{

    /** @var string $post_type_name custom post type name */
    public $post_type_name;
    /** @var array $labels label of custom post type */
    public $labels;

    /**
     * @param string $post_type_name
     * @param array $labels custom post type labels
     */
    public function __construct( $post_type_name, $labels = array() )
    {
        $this->post_type_name =  strtolower( str_replace(' ', '_', $post_type_name ) );
        $this->setLabels( $labels );

        if ( ! post_type_exists( $this->post_type_name ) ) {
            add_action( 'init', array( &$this, 'register') );
        }

        $this->save();
    }

    /**
     * set labels of custom post type
     *
     * @param $labels custom post type labels
     */
    private function setLabels( $labels )
    {
        $this->labels = array_merge(
            array(
                'name'               => 'Custom Post',
                'singular_name'      => 'Custom Post一覧',
                'add_new'            => 'Custom Postを追加',
                'add_new_item'       => 'Custom Postを追加',
                'edit_item'          => 'Custom Postを編集',
                'new_item'           => '新しいCustom Post',
                'view_item'          => 'Custom Postを表示',
                'search_items'       => 'Custom Postを探す',
                'not_found'          => 'Custom Postはありません',
                'not_found_in_trash' => 'ゴミ箱にCustom Postはありません',
            ),
            $labels
        );
    }

    /**
     * add custom taxonomy to custom post type
     *
     * @param string $name taxonomy name
     * @param array  $args register_taxonomy third parameter array
     */
    public function addTaxonomy( $name, $args = array() )
    {
        if ( ! true === empty( $name ) ) {
            $post_type_name   = $this->post_type_name;
            $taxonomy_name    = strtolower( str_replace(' ', '_', $post_type_name ) );
            $taxonomy_args    = $this->setTaxonomyArgs( $args );
        }

        if ( ! true === taxonomy_exists( $name ) ) {
            // register taxonomy
            add_action(
                'init',
                function() use( $taxonomy_name, $post_type_name, $taxonomy_args )
                {
                    register_taxonomy( $taxonomy_name, $post_type_name, $taxonomy_args );
                }
            );
        } else {
            add_action(
                'init',
                function() use( $taxonomy_name, $post_type_name )
                {
                    register_taxonomy_for_object_type( $taxonomy_name, $post_type_name );
                }
            );
        }
    }

    /**
     * set custom taxonomy arguments from labels
     *
     * @param  array $args custom taxonomy array of register_taxonomy third parameter
     * @return array custom taxonomy new argument array
     */
    private function setTaxonomyArgs( $args )
    {
        return array_merge(
            array(
                'label'                 => 'Custom Taxonomy',
                'singular_label'        => 'Custom Taxonomy',
                'hierarchical'          => false,
                'update_count_callback' => '_update_post_term_count',
            ),
            $args
        );
    }

    /**
     * add meta box to custom post type
     *
     * @param string $title custom meta box title
     * @param array  $fields field of custom meta box
     * @param string $context
     * @param string $priority
     */
    public function addMetabox( $title, $fields = array(), $context = 'normal', $priority = 'default')
    {
        if ( ! true === empty( $title ) ) {
            $post_type_name   = $this->post_type_name;
            $box_id           = strtolower( str_replace( ' ', '_', $title ) );
            $box_title        = ucwords( str_replace( '_', ' ', $title ) );
            $box_context      = $context;
            $box_priority     = $priority;

            global $custom_fields;
            $custom_fields[ $title ] = $fields;

            add_action(
                'admin_init',
                function() use( $box_id, $box_title, $post_type_name, $box_context, $box_priority, $fields )
                {
                    add_meta_box(
                        $box_id,
                        $box_title,
                        function( $post, $data )
                        {
                            global $post;
                            wp_nonce_field( 'save', 'meta_box' );
                            $custom_fields = $data[ 'args' ][ 0 ];
                            $meta = get_post_custom( $post->ID );

                            // Check the array and loop through it
                            if( ! true === empty( $custom_fields ) )
                            {
                                /* Loop through $custom_fields */
                                foreach( $custom_fields as $label => $type )
                                {
                                    $field_id_name  = strtolower( str_replace( ' ', '_', $data[ 'id' ] ) )
                                        . '_' . strtolower( str_replace( ' ', '_', $label ) );

                                    if ( true === isset( $meta[ $field_id_name ][ 0 ] ) ) {
                                        $value = $meta[ $field_id_name ][ 0 ];
                                    } else {
                                        $value = '';
                                    }
                                    echo '<label for="' . $field_id_name . '">' . $label
                                        . '</label><input type="text" name="custom_meta[' . $field_id_name . ']" id="'
                                        . $field_id_name . '" value="' . $value  . '" />';
                                }
                            }

                        },
                        $post_type_name,
                        $box_context,
                        $box_priority,
                        array( $fields )
                    );
                }
            );
        }
    }

    /**
     * save custom post type fields
     */
    private function save()
    {
        $post_type_name = $this->post_type_name;
        add_action(
            'save_post',
            function() use( $post_type_name )
            {
                if( defined( 'DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
                    return;
                }

                if ( false === isset( $_POST[ 'meta_box' ] ) ) {
                    return;
                }
                if ( ! wp_verify_nonce( $_POST[ 'meta_box' ], 'save' ) ) {
                    return;
                }
                global $post;
                if( isset( $_POST )
                    && isset( $post->ID )
                    && get_post_type( $post->ID ) === $post_type_name )
                {
                    global $custom_fields;

                    // Loop through each meta box
                    foreach( $custom_fields as $title => $fields )
                    {
                        // Loop through all fields
                        foreach( $fields as $label => $type )
                        {
                            $field_id_name  = strtolower( str_replace( ' ', '_', $title ) ) . '_' . strtolower( str_replace( ' ', '_', $label ) );
                            update_post_meta( $post->ID, $field_id_name, $_POST['custom_meta'][$field_id_name] );
                        }

                    }
                }

            }
        );
    }

    /**
     * register custom post type
     * not private but public
     */
    public function register()
    {
        $args = array(
            'labels'               => $this->labels,
            'public'               => true,
            'capability_type'      => 'post',
            'show_ui'              => true,
            'hierarchical'         => false,
            'supports'             => array( 'title', 'editor', 'thumbnail' ),
            'rewrite'              => array( 'slug' => $this->post_type_name, 'with_front' =>false ),
        );
        register_post_type( $this->post_type_name, $args );
    }

    /**
     * add column to costom  post type list
     *
     * @param string $key   field key custom post type filed name
     * @param string $label display label of field key
     */
    public function addTaxonomyColumn( $key, $label ) {

        $post_type_name = $this->post_type_name;

        // 一覧画面にカスタムタクソノミースタッフによる分類を追加
        $manage_edit_columns = function( $columns ) use( $key, $label )
        {
            /** @var string $key */
            /** @var string $label */
            $columns[ $key ] = $label;
            return $columns;
        };

        $manage_posts_column = function ( $column, $post_id ) use ( $key, $post_type_name )
        {
            /** @var string $key */
            /** @var string $post_type_name */
            if ( $key == $column ) {
                $value = get_the_term_list( $post_id, $post_type_name );
                $value = preg_replace( "/<[^>]+>(.+)<\/a>/", "$1", $value );
                echo $value;
            }
        };
        add_filter( 'manage_edit-' . $this->post_type_name . '_columns', $manage_edit_columns );
        add_action( 'manage_posts_custom_column', $manage_posts_column, 10, 2 );

    }

    public function addThumbnailColumn( $label )
    {
        $post_type_name = $this->post_type_name;
        // 一覧画面にカスタムタクソノミースタッフによる分類を追加
        $manage_edit_columns = function( $columns ) use( $label )
        {
            /** @var string $label */
            $columns[ 'thumbnail' ] = $label;
            return $columns;
        };
        $manage_posts_column = function ( $column, $post_id )
        {
            if ('thumbnail' == $column) {
                $value = get_the_post_thumbnail( $post_id, array( 100, 100 ) );
                echo $value;
            }
        };
        add_filter( 'manage_edit-' . $this->post_type_name . '_columns', $manage_edit_columns );
        add_action( 'manage_posts_custom_column', $manage_posts_column, 10, 2 );
    }

}

PHP,Programming,WordPress | 固定リンク | Comments (0)

このページの上へ移動

コメント

No comments yet.

コメントの投稿

改行と段落タグは自動で挿入されます。
メールアドレスは表示されません。

 

 

 


このページの上へ