PHP - Wordpress插件,用于将自定义分类添加到自定义分类

时间:2018-02-19 21:22:41

标签: php wordpress

Php - 我正在尝试创建一个带有自定义分类(Property Status)的自定义插件(Property),我需要两个预定义的类别(销售和租赁)。我能够创建帖子和分类。我需要帮助创建两个预定义的类别。 我尝试在自定义分类法中添加wp_set_post_category,但这没有用。任何帮助都将非常感激。我还在学习php而不确定如何解决这个问题。代码如下:

   <?php 
/**
* @package AahiPlugin
*/
/*
Plugin Name: Aahi Plugin
Plugin URI:
Description: Custom Plugin for IdeaTree
Author: Aahi Awasthee
Version: 1.0.0
Author URI: https://aahiawasthee.github.io/
License: GPLv2 or later
Text Domain: aahi-plugin
*/

defined('ABSPATH')  or die('Hey, what are you doing here?');

class AahiPlugin
{
    function __construct(){ 
        add_action( 'init', array($this, 'create_property'));
        add_action('init',array($this,'add_property_status_taxonomy'));
    }

    function activate(){
        //generated a Custom Post Type
        $this->create_property();
        //flush rewrite rules
        flush_rewrite_rules();
    }

    function deactivate(){
       //flush rewrite rules
        flush_rewrite_rules();
    }

    // Create Custom Post Type - Property
    function create_property(){
        register_post_type('property_type',        
        array(
            'labels' => array(
            'name' => 'Properties',
            'singular_name' => 'Property ',
            'add_new' => 'Add New',
            'add_new_item' => 'Add New Property',
            'edit' => 'Edit',
            'edit_item' => 'Edit Property',
            'new_item' => 'New Property',
            'view' => 'View',
            'view_item' => 'View Property',
            'search_items' => 'Search Property',
            'not_found' => 'No Property found',
            'not_found_in_trash' => 'No Properties found in Trash',
            'parent' => 'Parent Property'
        ),

        'public' => true,
        'menu_position' => 5,
        'supports' => array( 'title', 'editor', 'comments', 'thumbnail', 'custom-fields' ),
        'taxonomies' => array( 'add_property_status_taxonomy_to_post' ),
        'menu_icon' => plugins_url( 'images/image.png', __FILE__ ),
        'has_archive' => true
        )
    );
    }

    //Creating Property Status Taxonomy in Propties Custom Post Type
    function add_property_status_taxonomy(){

    //set the name of the taxonomy
    $taxonomy = 'Property Status';
    //set the post types for the taxonomy
    $object_type = 'property_type';

    //populate our array of names for our taxonomy
    $labels = array(
        'name'               => 'Property Status',
        'singular_name'      => 'Property Status',
        'search_items'       => 'Search Property Status',
        'all_items'          => 'All Property Status',
        'parent_item'        => 'Parent Property Status',
        'parent_item_colon'  => 'Parent Property Status:',
        'update_item'        => 'Update Property Status',
        'edit_item'          => 'Edit Property Status',
        'add_new_item'       => 'Add New Property Status', 
        'new_item_name'      => 'New Property Status Name',
        'menu_name'          => 'Property Status'
    );

    //define arguments to be used 
    $args = array(
        'labels'            => $labels,
        'hierarchical'      => true,
        'show_ui'           => true,
        'how_in_nav_menus'  => true,
        'public'            => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array('slug' => 'property-status')
    );

    //call the register_taxonomy function
    register_taxonomy($taxonomy, $object_type, $args); 

    }

    //Creating Property Status: Sale and Rent in Propties Custom Post Type

}

if(class_exists('AahiPlugin')){
$aahiPlugin = new AahiPlugin();
}

//activation
register_activation_hook(__FILE__, array($aahiPlugin, 'activate'));

//deactivation
register_deactivation_hook(__FILE__, array($aahiPlugin, 'deactivate'));


//uninstall
?>

1 个答案:

答案 0 :(得分:0)

分类法&#39; register_post_type()中的设置接受一系列分类名称 - 即,与您在$object_type中进一步设置的名称相同。

因此,该行应为'taxonomies' => array( 'property_type' ),

请参阅codex entry

希望有所帮助