简体   繁体   中英

How can I display custom posts as a sortable table with PHP for WordPress?

Ideally, I'm trying to do this with a plugin and then assign the table to a shortcode which I can use anywhere on a website.

Here's my generic code to add a custom post type:

    <?PHP

function create_post_type_customers(){
    // Set UI labels for CPT
    $labels = array(
        'name'                  =>  __('Customers'),
        'singular_name'         =>  __('Customer'),
        'menu_name'             =>  __('Customers'),
        'parent_item_colon'     =>  __('All Customers'),
        'all_items'             =>  __('All Customers'),
        'view_item'             =>  __('View Customer'),
        'add_new_item'          =>  __('Add New Customer'),
        'add_new'               =>  __('Add New'),
        'edit_item'             =>  __('Edit Customer'),
        'update_item'           =>  __('Update Customer'),
        'search_items'          =>  __('Search Customers'),
        'not_found'             =>  __('Not Found'),
        'not_found_in_trash'    =>  __('Not Found in Trash'),
    );
    
    // CPT Options
    $args = array(
        'label'                 =>  __('customers'),
        'description'           =>  __('Freedom Search Customers'),
        'labels'                =>  $labels,
        'supports'              =>  array('title', 'editor', 'revisions', 'custom-fields'), 
        'taxonomies'            =>  array('test'),  
        'hierarchical'          =>  false,
        'public'                =>  true,
        'show_ui'               =>  true,
        'show_in_menu'          =>  true,
        'show_in_nav_menu'      =>  true,
        'show_in_admin_bar'     =>  true,
        'menu_position'         =>  5,
        'has_archive'           =>  true,
        'rewrite'               =>  array('slug'    =>  'customers'),
        'exclude_from_search'   =>  false,
        'publicly_queryable'    =>  true,
        'capability_type'       =>  'post',
        'show_in_rest'          =>  true,
    );
    
    register_post_type('Customers', $args);
}

    add_action('init', 'create_post_type_customers', 0);

?>

I am fairly new to PHP and WordPress development, so some general direction would be brilliant, if not a full solution.

Note - register_post_type('Customers', $args); make Customers lowercase

Here is a simple example how to start

// Call shortcode by echo do_shortcode('[customers]') or [customers]

add_shortcode( 'customers', 'customers_shortcode' );
function customers_shortcode() {
    $args = array(
        'post_type' => 'customers',
        'posts_per_page' => -1,
        'post_status' => 'publish',
        'orderby' => 'DATE',
        'order' => 'DESC',
    );

    $customers = new WP_Query($args);

    if($customers->have_posts()):
        while($customers->have_posts()): $customers->the_post();
            //Do stuff here 
            the_title();
        endwhile;
    endif;
    wp_reset_query();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM