php Quick Singleton类将包含在WordPress插件(或主题functions.php文件)中,它将创建一个简单的Person帖子类型并显示

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Quick Singleton类将包含在WordPress插件(或主题functions.php文件)中,它将创建一个简单的Person帖子类型并显示相关的知识,希望对你有一定的参考价值。

<?php
class Sizeable_Person
{
    const POST_TYPE_SLUG = 'szbl-person';
    
    public static $instance;
    
    public static function init()
    {
        if ( is_null( self::$instance ) )
            self::$instance = new Sizeable_Person();
        return self::$instance;
    }
    
    private function __construct()
    {
        add_action( 'init', array( $this, 'register_post_type' ) );
        add_filter( 'enter_title_here', array( $this, 'enter_title_here' ) );
        add_action( 'save_post', array( $this, 'save_post' ) );
    }
    
    public function enter_title_here( $title )
    {
        if ( self::POST_TYPE_SLUG == get_post_type() )
            $title = 'Enter First and Last Name'; 
            
        return $title;
    }
    
    public function register_post_type()
    {
        $args = array(
            'label' => 'People',
            'public' => true,
            'show_ui' => true,
            'show_in_menu' => true,
            'supports' => array( 'title', 'editor', 'custom-fields' )
        );

        /*
            The following line allows other developers to modify the
            post type settings via plugin – super flexible!
        */
        $args = apply_filters( 'szbl_people-post_type_args', $args );

        register_post_type( self::POST_TYPE_SLUG, $args );
    }
    
    public function save_post( $post_id )
    {
        // do something on save, such as store custom meta data
    }
    
    public static function get_person_by( $field, $value )
    {
        $args = array(
            'post_type' => self::POST_TYPE_SLUG,
            'orderby' => 'menu_order',
            'order' => 'asc',
            'posts_per_page' => 999
        );
        
        switch ( $field )
        {
            case 'name':
            case 'title':
                return get_page_by_title( $value, OBJECT, self::POST_TYPE_SLUG );
                break;

            case 's':
                $args['s'] = $value;
                break;

            // Assumes we're looking for post meta save by this plugin
            // and that the keys have a prefix of "szbl_person_"
            default:
                $args['meta_query'] = array(
                    array(
                        'key' => 'szbl_person_' . $field,
                        'value' => $value
                    )
                );
                break;
        }

        $args = apply_filters( 'szbl_people-get_person_by_args', $args );

        return get_posts( $args );
    }
    
}

// initialize all this code
Sizeable_Person::init();

/*
    As a Singleton, we're only going to have 1 object in memory, ever,
    which is all we need!

    Now we can call something like this from anywhere to 
    get data from this object as well such as the get_person_by() 
    function...
    
    Example:
    
        $person = Sizeable_Person::get_person_by( 'name', 'Bill Burr' );
        $person = Sizeable_Person::get_person_by( 'email', 'andy@sizeableinteractive.com' );
        
    Returns:
    
        Object (post) where ( $person->post_type == 'szbl-person' )
        
        $person_email = get_post_meta( $person->ID, 'szbl_person_email', true );
    
*/

以上是关于php Quick Singleton类将包含在WordPress插件(或主题functions.php文件)中,它将创建一个简单的Person帖子类型并显示的主要内容,如果未能解决你的问题,请参考以下文章

扩展银行项目,添加一个(客户类)Customer类。Customer类将包含一个Account对象。

PHP PHP5 Singleton类

PHP PHP Singleton类

php Wzorzec Singleton

用lua实现ByteArray和ByteArrayVarint

项目错误:QT 中的未知模块:qml quick